diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29e1552..f79a564 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,7 @@ jobs: - run: python -m pip install torch --index-url https://download.pytorch.org/whl/cpu - run: python -m pip install . pytest - run: python -m pytest -q tests/test_contraction_compiler.py tests/test_distributed_compute.py + - run: python -m pytest -q tests/test_hybrid_qnm.py -k distributed_hybrid_qnm hybrid-quant-compiler: runs-on: ubuntu-latest timeout-minutes: 12 @@ -51,7 +52,7 @@ jobs: python-version: '3.12' - run: python -m pip install torch --index-url https://download.pytorch.org/whl/cpu - run: python -m pip install ".[jax,qiskit,dev]" - - run: python -m pytest -q tests/test_submission_training.py tests/test_expectile_risk.py tests/test_classical_arithmetic.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_classical_predicates.py tests/test_compilation_portability.py tests/test_package_layout.py + - run: python -m pytest -q tests/test_distance_aware.py tests/test_execution_risk.py tests/test_hybrid_qnm.py tests/test_submission_training.py tests/test_expectile_risk.py tests/test_integer_division.py tests/test_classical_arithmetic.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_classical_predicates.py tests/test_compilation_portability.py tests/test_package_layout.py public-types: runs-on: ubuntu-latest steps: @@ -124,7 +125,7 @@ jobs: - run: python -m pip install "torch==2.2.2" --index-url https://download.pytorch.org/whl/cpu - run: python -m pip install "numpy==1.26.4" "jax==0.4.30" "jaxlib==0.4.30" "optax==0.2.3" "chex==0.1.87" - run: python -m pip install ".[dev]" - - run: python -m pytest -q tests/test_submission_training.py tests/test_expectile_risk.py tests/test_compilation_portability.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_native_fusion.py tests/test_execution_hotpaths.py tests/test_package_layout.py tests/test_compute_kernel.py tests/test_reliability.py tests/test_entry_contracts.py tests/test_prepared_workflows.py tests/test_register_programs.py tests/test_accelerators.py tests/test_native_gpu.py tests/test_native_constant_cache.py + - run: python -m pytest -q tests/test_distance_aware.py tests/test_execution_risk.py tests/test_hybrid_qnm.py tests/test_submission_training.py tests/test_expectile_risk.py tests/test_compilation_portability.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_native_fusion.py tests/test_execution_hotpaths.py tests/test_package_layout.py tests/test_compute_kernel.py tests/test_reliability.py tests/test_entry_contracts.py tests/test_prepared_workflows.py tests/test_register_programs.py tests/test_accelerators.py tests/test_native_gpu.py tests/test_native_constant_cache.py compiled-transforms: runs-on: ubuntu-latest steps: @@ -134,7 +135,7 @@ jobs: python-version: '3.11' - run: python -m pip install "torch==2.4.1" --index-url https://download.pytorch.org/whl/cpu - run: python -m pip install "numpy==1.26.4" . pytest - - run: python -m pytest -q tests/test_submission_training.py tests/test_expectile_risk.py tests/test_compilation_portability.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_native_fusion.py tests/test_activation_checkpointing.py tests/test_execution_hotpaths.py tests/test_package_layout.py + - run: python -m pytest -q tests/test_distance_aware.py tests/test_execution_risk.py tests/test_hybrid_qnm.py tests/test_submission_training.py tests/test_expectile_risk.py tests/test_compilation_portability.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_native_fusion.py tests/test_activation_checkpointing.py tests/test_execution_hotpaths.py tests/test_package_layout.py portable-install: strategy: fail-fast: false @@ -174,7 +175,7 @@ jobs: } - run: python -m pip check - run: python -m openqml doctor --extra all --check-imports --sizes --output environment-report.json - - run: python -m pytest -q tests/test_submission_training.py tests/test_expectile_risk.py tests/test_classical_arithmetic.py tests/test_compilation_portability.py tests/test_native_fusion.py tests/test_register_programs.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_classical_predicates.py + - run: python -m pytest -q tests/test_distance_aware.py tests/test_execution_risk.py tests/test_hybrid_qnm.py tests/test_submission_training.py tests/test_expectile_risk.py tests/test_integer_division.py tests/test_classical_arithmetic.py tests/test_compilation_portability.py tests/test_native_fusion.py tests/test_register_programs.py tests/test_native_geometry.py tests/test_transport_geometry.py tests/test_classical_predicates.py - run: python -m pytest -q tests/test_compute_kernel.py tests/test_reliability.py tests/test_entry_contracts.py tests/test_prepared_workflows.py tests/test_accelerators.py tests/test_native_gpu.py tests/test_precision_regressions.py tests/test_audit_regressions.py tests/test_switch_lowering.py tests/test_risk_budget.py tests/test_tensor_streaming.py tests/test_model_checkpoint.py tests/test_model_selection.py tests/test_walk_forward.py tests/test_conversion_sweeps.py tests/test_finance_execution.py tests/test_simple_api.py tests/test_adaptive_amplitude.py tests/test_amplitude.py tests/test_finance.py tests/test_frontier_models.py tests/test_algorithmic_execution.py tests/test_network_operators.py tests/test_architecture.py tests/test_deep_learning.py tests/test_cost_scheduling.py tests/test_calibration.py tests/test_distributed_compute.py tests/test_multiscale_execution.py tests/test_gradient_accumulation.py tests/test_program_compilation.py tests/test_distributed_training.py tests/test_compute_contractions.py tests/test_contraction_compiler.py tests/test_runtime.py tests/test_hybrid_training.py tests/test_sparse_scaling.py tests/test_research_methods.py tests/test_public_api.py tests/test_package_scope.py tests/test_package_layout.py tests/test_execution_hotpaths.py tests/test_mitigation.py tests/test_model_streaming.py tests/test_backends.py tests/test_providers.py tests/test_shared_readouts.py tests/test_conversion_plan.py tests/test_resource_audit.py tests/test_source_boundaries.py tests/test_resource_profiles.py tests/test_environment_report.py tests/test_scientific_scale.py tests/test_semantic_compiler.py tests/test_adaptive_execution.py - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 if: always() diff --git a/.github/workflows/gpu-integration.yml b/.github/workflows/gpu-integration.yml index e2f942a..5ed2b33 100644 --- a/.github/workflows/gpu-integration.yml +++ b/.github/workflows/gpu-integration.yml @@ -42,7 +42,7 @@ jobs: OPENQML_HARDWARE_REPORT_DIR: ${{ runner.temp }}/openqml-hardware run: python -m pytest -q tests/test_distributed_compute.py -k physical_multi_gpu - name: Verify resident native quantum geometry - run: python -m pytest -q tests/test_native_geometry.py -k physical_cuda + run: python -m pytest -q tests/test_native_geometry.py tests/test_distance_aware.py -k physical_cuda - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 if: always() with: diff --git a/MANIFEST.in b/MANIFEST.in index 160164b..d48af3c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ -include README.md LICENSE CONTRIBUTING.md GOVERNANCE.md SECURITY.md CODE_OF_CONDUCT.md +include README.md LICENSE +exclude CONTRIBUTING.md GOVERNANCE.md SECURITY.md CODE_OF_CONDUCT.md prune tests prune docs prune .github diff --git a/README.md b/README.md index f3346e2..158b6ee 100644 --- a/README.md +++ b/README.md @@ -8,21 +8,20 @@ Build and train hybrid neural networks with PyTorch, JAX or Flax. Use `Model.fit` and `predict` for supervised tasks, or compose convolution, attention, graph, recurrent and generative architectures with native layers. Validation loss, early stopping and portable checkpoints keep model selection and continued training in the same workflow. [Explore hybrid AI](https://github.com/OpenQML-org/OpenQML/blob/main/docs/models.md). -Native quantum metric products and certified natural-gradient solves retain -PyTorch/JAX tensors and device placement. An optional geometry-first spectral -step balances circuit parameter blocks after preconditioning. -External Torch/JAX layers can pack forward and parameter-shift rows together, -reusing one linearization during backward to reduce provider round trips. +Train native quantum blocks with resumable, distance-aware Quantum Natural Muon +and classical blocks with AdamW through one `hybrid_qnm` optimizer. Microbatch +accumulation and DDP share an empirical, matrix-free quantum Fisher metric. +External Torch/JAX layers can pack forward and parameter-shift rows into one +provider submission and reuse their linearization during backward. ## Quantum–classical hybrid quantitative research Train scenario distributions, allocate constrained portfolios and project decisions onto explicit CVaR budgets with a funded cash position. Low-rank factor solves and native PyTorch/JAX derivatives connect financial objectives to hybrid models. Purged walk-forward refits and self-financing accounting preserve chronological evaluation, execution delays, fees and market impact. [Explore quantitative research](https://github.com/OpenQML-org/OpenQML/blob/main/docs/finance.md). -Learn contextual Wasserstein radii and anisotropic transport metrics alongside -quantum scenario probabilities. Factor-space implicit derivatives connect the -robust risk bound and funded cash allocation to the predictive model. -Coherent expectile risk and Wasserstein expectile budgets provide an additional -native decision objective with exact sorted roots. +Learn scenario probabilities, Wasserstein radii and anisotropic transport metrics. +`ScenarioPlan.execution_loss` connects proper-score calibration to after-cost +decisions. Exact piecewise quadratic projections fund proportional fees and +market impact while enforcing CVaR or expectile budgets. ## Cross-framework quantum programming @@ -31,8 +30,9 @@ Connect Qiskit, Cirq, PennyLane and OpenQASM through a shared circuit representa Typed Qiskit logical, bitwise and unsigned comparison predicates lower to bounded shared decision graphs. Their branch selection completes before either payload executes, including payloads that overwrite a measured predicate input. -Fixed-width unsigned arithmetic, bounded shifts and dynamic bit indexing retain -the same atomic branch-selection contract. +Fixed-width unsigned arithmetic includes division with symbolically proved +nonzero divisors. Correlated selector proofs admit bounded dynamic shifts and +bit indexing while preserving atomic branch selection. Simulate physical noise with amplitude/phase damping, depolarization and custom Kraus channels. Correct independent readout errors or extrapolate measured noise scales with explicit conditioning and sampling-overhead limits. Caller-initialized Qiskit V2 primitives connect real hardware; see [execution limits and mitigation](https://github.com/OpenQML-org/OpenQML/blob/main/docs/runtime.md#physical-noise-and-error-mitigation). diff --git a/docs/api.md b/docs/api.md index cfda725..edd6651 100644 --- a/docs/api.md +++ b/docs/api.md @@ -46,6 +46,18 @@ operate on native Torch/JAX parameter vectors. Their result types are `NativeMetricResult` and `SpectralStepResult`. See [native quantum geometry](native-geometry.md) for the metric convention, spectral block order, residual certificates and scope. +`DistanceAwareQNM` returns `DistanceAwareResult(step, state, natural, radius, +alignment, restarted)`. `DistanceAwareState` is an explicit native tensor tree. +`hybrid_qnm(model, ...)` creates a Torch optimizer for native unitary +`QuantumLayer` weights and classical parameters. It is also available from +`openqml.autodiff`; importing its factory does not load Torch. + +`finance.execution_risk_projection` returns `ExecutionRiskResult`, including +pre-trade positions, post-cost target weights, cash, costs and risk certificates. +`ScenarioPlan.execution_decision` supplies its scenario atoms; +`execution_loss` returns a `ScenarioDecisionResult` containing the decision, +per-example objective, calibration score and realized net return. + `ShiftPlan.linearize` returns `ShiftLinearization` with forward values, an explicit Jacobian and submission counts. External Torch/JAX layers opt in with `gradient_method="batched-shift"`; see [submission-aware training](submission-training.md). diff --git a/docs/finance.md b/docs/finance.md index e83c014..8784e42 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -554,6 +554,85 @@ over known probabilities is O(S). Compare complete workloads including table loading, transpilation, error correction, queue time and classical variance reduction or quasi-Monte-Carlo execution. +## Execution-cost-aware scenario decisions + +`execution_risk_projection` finds the largest feasible scale `s` of a funded, +long-only proposal. Unlike a risk-only cash allocation, its constraints include +proportional fees and quadratic market impact from current holdings. For +pre-trade NAV fractions `w` and `previous`, trades are `s*w - previous`, costs +are `C(s)=sum(fees*abs(trades)+impact*trades**2)`, and cash is +`1-s*sum(w)-C(s)`. The CVaR or coherent expectile budget applies to the +**after-cost** loss, including the return on the remaining cash. + +```python +import openqml as oq + +plan = oq.finance.ScenarioPlan( + [ + [-0.12, -0.03], + [0.04, -0.08], + [0.10, 0.02], + [-0.05, 0.01], + ] +) +decision = plan.execution_decision( + [0.25, 0.25, 0.25, 0.25], + [0.4, 0.5], + previous=[0.3, 0.2], + fees=0.001, + impact=0.01, + confidence=0.8, + budget=0.015, + radius=0.001, +) +print(decision.weights, decision.cash, decision.costs, decision.robust_risk) +``` + +`positions`, `cash` and `costs` are fractions of pre-trade NAV and sum to one. +`weights=positions/(1-costs)` are **post-cost** target weights, the units accepted +by `SelfFinancingPlan`. Using `positions` as its target would charge different +trades. CVaR uses `confidence` in `[0,1)`; coherent expectile mode uses +`risk_measure="expectile"` and `confidence` in `[0.5,1)`. An anisotropic +`transport_metric` uses the same native dual-norm operator as the existing +Wasserstein risk layers. + +Sorted trade sign changes partition `C(s)` into convex quadratics. Closed-form, +scale-stable quadratic roots locate the binding risk or funding constraint in +O(A log A + S log S) work, with linear workspace for A assets and S scenarios. +There is no optimization-iteration tape or dense asset matrix. Torch/JAX retain +gradients through probabilities, proposals, holdings, costs, radius and transport +parameters, away from active-set, ordering and norm singularities. Zero initial +impact retains its one-sided derivative. Returned risk and funding constraints +are checked against native numerical tolerances. + +Both proposal and current holdings must be funded and long-only; fees and +impact must be nonnegative, with `max(fees)+2*max(impact)<1`. Scenarios and cash +returns are arithmetic returns of at least -1. Full liquidation must itself meet +funding and the risk budget. If liquidation is infeasible, the operator raises +`ValidationError`; it does not assume that fees can be borrowed. The feasible +scale interval is convex under these conditions. + +For decision-focused hybrid training, call +`plan.execution_loss(probabilities, proposal, observations, ...)`. Its per-example +loss is `calibration_weight*energy_score - decision_weight*realized_net_return`. +Predicted probabilities alone determine the scenario risk decision; observations +enter only the proper calibration score and realized training objective. Native +gradients connect both a quantum Born distribution and a classical allocation +head to this loss. A temporal scenario plan with no `step` describes one initial +trade held over the compounded horizon. Observations and `cash_return` must +refer to that same horizon; `execution_loss` rejects a partial-step decision. + +This certifies the supplied discrete model and cost assumptions. Recompute from +marked holdings and current cost estimates at fill time if an execution delay +changes them. Future scenario errors, liquidity and delayed price moves are not +covered by an earlier decision's certificate. No orders are submitted and no +investment-performance or quantum-speedup claim follows from these operators. + +The integrated predictive/decision objective follows the direction of +[Financially Guided Deep Portfolio Optimization](https://arxiv.org/abs/2605.28853), +with OpenQML's own exact cash-ray projection. It does not reproduce that paper's +full model or reported market results. + ## Method sources - [Learning Predictive Ambiguity Sets for Decision-Focused Distributionally diff --git a/docs/hybrid-training.md b/docs/hybrid-training.md index 30a71ae..daedbef 100644 --- a/docs/hybrid-training.md +++ b/docs/hybrid-training.md @@ -150,6 +150,69 @@ topology, chunk equivalence, Torch compile/func transforms and JAX JIT/vmap/Opta CUDA tests execute when hardware is available. Measure throughput on the target device with the intended architecture, precision, batch size and execution path. +## Joint AdamW and Quantum Natural Muon + +`hybrid_qnm` owns a complete Torch module. Classical parameters use AdamW; +active weights of native, unitary `QuantumLayer` modules use the stateful +distance-aware quantum Fisher method. For example, after constructing a model +with a quantum submodule named `quantum` containing four active weights: + +```python +import openqml as oq +from openqml.qml.interfaces.torch import HybridTrainer + +optimizer = oq.hybrid_qnm( + model, + lr=1e-3, + quantum_lr=0.02, + block_shapes={"quantum": ((2, 2),)}, + quantum_options={"damping": 0.05, "momentum": 0.9}, +) +trainer = HybridTrainer(model, optimizer, microbatch_size=16) + + +def objective(network, batch): + features, targets = batch + return (network(features) - targets).square().mean() + + +trainer.step((features, targets), objective) +trainer.save("training.pt") +optimizer.close() +``` + +Only layer calls participating in backward contribute geometry. Unused forward +passes and inference do not enter the empirical metric. Feature snapshots span +all participating calls and microbatches until `step` or `zero_grad`; their +memory is admitted against layer and shared compute budgets. Do not mutate +inputs between forward and backward. This metric uses uniform mass per captured +feature row, independently of loss weighting; repeated uses contribute repeated +rows. Classical layers influence the loss gradient but their full cross-block +Fisher information is not computed. + +For `DistributedDataParallel`, construct the optimizer with the wrapped model. +Block-shape keys follow its module names, such as `module.quantum`. Metric-vector +products are reduced with each rank's actual feature-row count. The same global +Fisher system is solved on each rank, without gathering a dense metric. +`HybridTrainer` also weights the objective by global sample count, including +unequal rank sizes. Ranks must execute the same optimizer groups and collectives. +This path uses simulator state derivatives, not finite-shot hardware geometry. + +Quantum master weights must use float32 or float64. `HybridTrainer` can use AMP +for the surrounding model, and its existing finite-gradient checks apply before +either optimizer updates. Quantum groups receive no AdamW weight decay; each +group's `lr` supports ordinary schedulers and zero-rate freezing. Spectral shapes +must cover active circuit coordinates in `weight_parameters` order. With no +shape mapping, each quantum vector uses a single-row block, retaining natural +momentum without nontrivial polar redistribution. + +`trainer.state_dict()` and its save/load helpers retain classical moments, +quantum momentum, initial coordinates, trajectory distance, counters, scheduler +and random state. Restore into the same circuit and optimizer configuration; +identity is checked before trainer model mutation. Call `optimizer.close()` +when replacing it to remove its hooks. Capturable, differentiable, fused and +maximizing optimizer steps are outside this integration. + ## Validation and early stopping The high-level `Model` interface provides a held-out loss monitor for Torch, diff --git a/docs/interoperability.md b/docs/interoperability.md index c9e9f19..b741ab2 100644 --- a/docs/interoperability.md +++ b/docs/interoperability.md @@ -84,7 +84,20 @@ selectors whose full bit-vector range is provably inside the operand width. Barrel-select networks lower dynamic selectors without enumerating valuations. Out-of-range or unproven selectors retain range diagnostics. -Standalone mutable variables, signed/float arithmetic, division, stores, timing +Unsigned division lowers to bounded restoring bit-vector division. The compiler +must symbolically prove the divisor nonzero for **every** classical input; +otherwise it raises `CLASSICAL_DIVISOR_DOMAIN`. Nonconstant divisors such as a +widened two-bit register plus one are admitted when the proof succeeds. +Division by a constant power of two simplifies to a logical shift. General +division charges quadratic bit work before expansion. No undefined zero-divisor +behavior is chosen and no logical short-circuit evaluation is assumed. + +Selector bounds are proved on the full symbolic bit vector, retaining +correlations between bits. A selector whose only possible values are 1 and 2 +can therefore index a three-bit word even when independent bit bounds would +allow 3. Predicate decisions still complete before either selected payload. + +Standalone mutable variables, signed/float arithmetic, stores, timing and other unsupported expression operations retain explicit diagnostics or require the existing native program route. diff --git a/docs/native-geometry.md b/docs/native-geometry.md index 8df6e55..5d0ce08 100644 --- a/docs/native-geometry.md +++ b/docs/native-geometry.md @@ -13,6 +13,7 @@ solve, and batched state evolution avoids a Python circuit call per example. | `native_matvec(bindings, vector, backend="torch")` | Exact real Fubini–Study metric times a vector | | `native_natural_gradient(bindings, gradient, backend="torch", ...)` | Native direction and a true damped-system residual certificate | | `spectral_step(bindings, gradient, block_shapes=..., ...)` | Bounded spectral-natural displacement and the original CG certificate | +| `DistanceAwareQNM(plan, block_shapes, ...).step(...)` | Stateful quantum Fisher momentum, active polar mixing and a trajectory-dependent displacement bound | The metric is `G = Re(J* (I - |psi> natural momentum -> active polar mixing -> distance radius. + + Uses F=4*Re(QGT). Selected coordinates are shared scalar bindings; auxiliary + circuit inputs may carry the existing weighted sample axis. State is an + explicit Torch/JAX tree, detached between steps and checked against this + circuit, parameter order and optimizer configuration. No parameters mutate. + """ + + plan: QuantumMetricPlan + block_shapes: tuple + damping: float = 0.01 + momentum: float = 0.9 + mixing: float = 0.1 + learning_rate: float = 0.01 + initial_radius: float = 0.01 + distance_growth: float = 0.1 + max_step_norm: float = 0.1 + normalization_epsilon: float = 1e-8 + tolerance: float = 1e-6 + max_iterations: int = 32 + alignment_guard: bool = True + _signature: tuple = field(init=False, repr=False) + + def __post_init__(self): + if not isinstance(self.plan, QuantumMetricPlan) or not self.plan.parameters: + raise ValidationError("Distance-aware QNM needs a nonempty QuantumMetricPlan.") + shapes = _spectral_shapes(self.block_shapes, len(self.plan.parameters)) + object.__setattr__(self, "block_shapes", shapes) + settings = {} + for name in ( + "damping", + "momentum", + "mixing", + "learning_rate", + "initial_radius", + "distance_growth", + "max_step_norm", + "normalization_epsilon", + "tolerance", + ): + value = real(getattr(self, name)) + settings[name] = value + object.__setattr__(self, name, value) + if not 0 <= self.momentum < 1 or not 0 <= self.mixing <= 1 or self.distance_growth < 0: + raise ValidationError( + "Use momentum in [0,1), mixing in [0,1] and nonnegative distance growth." + ) + if any( + settings[n] <= 0 + for n in ( + "damping", + "learning_rate", + "initial_radius", + "max_step_norm", + "normalization_epsilon", + "tolerance", + ) + ): + raise ValidationError( + "QNM damping, rates, radii, epsilon and tolerance must be positive." + ) + object.__setattr__( + self, "max_iterations", integer(self.max_iterations, "max_iterations", 1) + ) + if type(self.alignment_guard) is not bool: + raise ValidationError("alignment_guard must be a boolean.") + payload = json.dumps( + [ + "openqml.distance-aware-qnm.v1", + self.plan.parameters, + shapes, + settings, + int(self.max_iterations), + self.alignment_guard, + ], + sort_keys=True, + ).encode() + digest = hashlib.sha256(payload) + digest.update( + self.plan.circuit.to_json( + indent=None, max_bytes=int(self.plan.max_memory_mb * 2**20) + ).encode() + ) + value = digest.digest() + object.__setattr__( + self, + "_signature", + tuple(int.from_bytes(value[i : i + 4], "little", signed=True) for i in range(0, 32, 4)), + ) + + @compute_operation + def step( + self, + bindings, + gradient, + *, + state=None, + backend="torch", + sample_weights=None, + learning_rate=None, + metric_reducer=None, + ): + """Return a displacement to subtract and the next immutable native state. + + The nested residual certifies (F+damping I)u=g, before momentum and polar + mixing. The optional safeguard restarts momentum from u if the proposed + direction lacks positive observed gradient alignment. It is not a line + search or a loss-decrease certificate. + """ + ops, local, gradient, mass = _inputs(self.plan, bindings, gradient, backend, sample_weights) + if any(local[name].ndim for name in self.plan.parameters): + raise ValidationError( + "Stateful QNM selected coordinates must be shared scalar bindings." + ) + xp = ops.xp + rate = ops.array( + self.learning_rate if learning_rate is None else learning_rate, real_value=True + ) + if rate.ndim: + raise ValidationError("The learning-rate override must be a scalar.") + rate = ops.check( + rate, + xp.isfinite(rate) & (rate >= 0), + "Learning-rate overrides must be finite and nonnegative.", + ) + if metric_reducer is not None and not callable(metric_reducer): + raise ValidationError("metric_reducer must be callable or None.") + coordinates = xp.stack(tuple(local[name] for name in self.plan.parameters)) + get_compute_kernel().admit( + 64 * len(self.plan.parameters) + 8 * sum(min(s) ** 2 for s in self.block_shapes), + self.plan.max_memory_mb, + ) + if backend == "torch": + signature = xp.tensor(self._signature, dtype=xp.int32, device=gradient.device) + counter = xp.zeros((), dtype=xp.int32, device=gradient.device) + else: + signature = xp.asarray(self._signature, dtype=xp.int32) + counter = xp.zeros((), dtype=xp.int32) + if state is None: + state = DistanceAwareState( + stop(coordinates, backend), + gradient * 0, + ops.array(0, real_value=True), + counter, + signature, + ) + state = self._check_state(state, gradient, signature, ops, backend) + natural = _natural_solve( + self.plan, + local, + gradient, + mass, + ops, + backend, + metric_scale=4, + metric_reducer=metric_reducer, + damping=self.damping, + tolerance=self.tolerance, + max_iterations=self.max_iterations, + ) + momentum = self.momentum * state.momentum + (1 - self.momentum) * natural.direction + direction = _polar_mix(momentum, self.block_shapes, self.mixing, ops, backend) + alignment = (gradient * direction).sum() + restart = (~(xp.isfinite(alignment) & (alignment > 0))) & self.alignment_guard + momentum = xp.where(restart, natural.direction, momentum) + direction = xp.where(restart, natural.direction, direction) + distance = xp.maximum(state.max_distance, ops.norm(coordinates - state.initial_parameters)) + radius = xp.minimum( + rate * ops.norm(momentum), + xp.minimum( + self.initial_radius + self.distance_growth * distance, + ops.array(self.max_step_norm, real_value=True), + ), + ) + displacement = radius * (direction / (ops.norm(direction) + self.normalization_epsilon)) + displacement = ops.check( + displacement, xp.isfinite(displacement), "QNM displacement exceeds finite precision." + ) + momentum = ops.check(momentum, xp.isfinite(momentum), "QNM momentum must remain finite.") + distance = ops.check( + distance, xp.isfinite(distance), "QNM trajectory distance must remain finite." + ) + next_state = DistanceAwareState( + stop(state.initial_parameters, backend), + stop(momentum, backend), + stop(distance, backend), + state.steps + 1, + signature, + ) + return DistanceAwareResult( + displacement, next_state, natural, radius, (gradient * displacement).sum(), restart + ) + + @staticmethod + def _check_state(state, gradient, signature, ops, backend): + if not isinstance(state, DistanceAwareState): + raise ValidationError("Provide DistanceAwareState from the same optimizer.") + shapes = (gradient.shape, gradient.shape, (), (), signature.shape) + valid = signature[0] == signature[0] + for i, (value, shape) in enumerate(zip(state, shapes, strict=True)): + dtype = gradient.dtype if i < 3 else ops.xp.int32 + if _array_backend(value) != backend or value.shape != shape or value.dtype != dtype: + raise ValidationError( + "QNM state shapes, framework and dtypes must match the current optimizer." + ) + if backend == "torch" and value.device != gradient.device: + raise ValidationError( + "Move all QNM state tensors explicitly to the selected device." + ) + valid = valid & ops.xp.all(ops.xp.isfinite(value)) + valid = ( + valid + & ops.xp.all(state.signature == signature) + & (state.max_distance >= 0) + & (state.steps >= 0) + ) + initial = ops.check( + state.initial_parameters, + valid, + "QNM state must be finite, have valid counters and match the circuit, " + "parameter order and optimizer configuration.", + ) + initial = ops.check( + initial, + state.steps < 2_147_483_647, + "QNM step counter is exhausted.", + error=ResourceError, + ) + # Construct directly: older Dynamo treats NamedTuple._replace as a + # skipped stdlib function even though all of its fields are native. + return DistanceAwareState( + initial, state.momentum, state.max_distance, state.steps, state.signature + ) diff --git a/src/openqml/autodiff/native_geometry.py b/src/openqml/autodiff/native_geometry.py index 2f662ff..6a4918e 100644 --- a/src/openqml/autodiff/native_geometry.py +++ b/src/openqml/autodiff/native_geometry.py @@ -156,17 +156,36 @@ def native_natural_gradient( plan, bindings, gradient, *, backend="torch", sample_weights=None, **options ): """Solve (Re(QGT)+damping*I)d=g on the selected native device.""" + ops, local, gradient, mass = _inputs(plan, bindings, gradient, backend, sample_weights) + return _natural_solve(plan, local, gradient, mass, ops, backend, **options) + + +def _natural_solve( + plan, local, gradient, mass, ops, backend, *, metric_scale=1, metric_reducer=None, **options +): from ..qml._native_training import solve_metric - ops, local, gradient, mass = _inputs(plan, bindings, gradient, backend, sample_weights) if not plan.parameters: raise ValidationError("A natural-gradient solve needs selected parameters.") - result = solve_metric( - lambda value: { - "coordinates": native_matvec( - plan, local, value["coordinates"], backend=backend, sample_weights=mass + + def product(value): + result = metric_scale * native_matvec( + plan, local, value["coordinates"], backend=backend, sample_weights=mass + ) + if metric_reducer is not None: + result = metric_reducer(result) + if ( + _array_backend(result) != backend + or result.shape != gradient.shape + or result.dtype != gradient.dtype + ): + raise ValidationError( + "Metric reducers must preserve the native vector shape and dtype." ) - }, + return {"coordinates": result} + + result = solve_metric( + product, {"coordinates": gradient}, ops, **options, @@ -174,6 +193,21 @@ def native_natural_gradient( return NativeMetricResult(result.direction["coordinates"], *result[1:]) +def _spectral_shapes(block_shapes, size): + try: + shapes = tuple( + tuple(integer(n, "spectral block dimension", 1) for n in shape) + for shape in block_shapes + ) + except TypeError as exc: + raise ValidationError("Spectral blocks require an iterable of matrix shapes.") from exc + if any(len(s) != 2 for s in shapes) or sum(a * b for a, b in shapes) != size: + raise ValidationError( + "Spectral matrix blocks must partition the selected parameter vector." + ) + return shapes + + @compute_operation def spectral_step( plan, @@ -198,17 +232,7 @@ def spectral_step( mixing, rate, radius = real(mixing), real(learning_rate), real(max_step_norm) if not 0 <= mixing <= 1 or rate <= 0 or radius <= 0: raise ValidationError("Use mixing in [0,1] and positive learning rate/step bound.") - try: - shapes = tuple( - tuple(integer(n, "spectral block dimension", 1) for n in shape) - for shape in block_shapes - ) - except TypeError as exc: - raise ValidationError("Spectral blocks require an iterable of matrix shapes.") from exc - if any(len(s) != 2 for s in shapes) or sum(a * b for a, b in shapes) != len(plan.parameters): - raise ValidationError( - "Spectral matrix blocks must partition the selected parameter vector." - ) + shapes = _spectral_shapes(block_shapes, len(plan.parameters)) ops, local, gradient, mass = _inputs(plan, bindings, gradient, backend, sample_weights) get_compute_kernel().admit( 32 * len(plan.parameters) + 8 * sum(min(s) ** 2 for s in shapes), plan.max_memory_mb @@ -216,9 +240,22 @@ def spectral_step( natural = native_natural_gradient( plan, local, gradient, backend=backend, sample_weights=mass, **options ) + xp = ops.xp + direction = _polar_mix(natural.direction, shapes, mixing, ops, backend) + alignment = (gradient * direction).sum() + fallback = ~(xp.isfinite(alignment) & (alignment > 0)) + direction = xp.where(fallback, natural.direction, direction) + norm = ops.norm(direction) + length = xp.minimum(rate * ops.norm(natural.direction), ops.array(radius, real_value=True)) + step = (direction / xp.where(norm > 0, norm, 1)) * length + step = ops.check(step, xp.isfinite(step), "Spectral natural step must be finite.") + return SpectralStepResult(step, natural, (gradient * step).sum(), fallback | (mixing == 0)) + + +def _polar_mix(vector, shapes, mixing, ops, backend): xp, parts, start = ops.xp, [], 0 for a, b in shapes if mixing else (): - matrix = natural.direction[start : start + a * b].reshape((a, b)) + matrix = vector[start : start + a * b].reshape((a, b)) u, singular, vh = xp.linalg.svd(matrix, full_matrices=False) # The partial polar factor must leave zero singular subspaces inactive. eps = 2.220446049250313e-16 if "64" in str(matrix.dtype) else 1.1920928955078125e-7 @@ -229,15 +266,7 @@ def spectral_step( scaled = polar * norm / xp.sqrt(xp.where(rank > 0, rank, 1)) parts.append(((1 - mixing) * matrix + mixing * scaled).reshape(-1)) start += a * b - direction = natural.direction if not mixing else _cat_parts(parts, backend, xp) - alignment = (gradient * direction).sum() - fallback = ~(xp.isfinite(alignment) & (alignment > 0)) - direction = xp.where(fallback, natural.direction, direction) - norm = ops.norm(direction) - length = xp.minimum(rate * ops.norm(natural.direction), ops.array(radius, real_value=True)) - step = (direction / xp.where(norm > 0, norm, 1)) * length - step = ops.check(step, xp.isfinite(step), "Spectral natural step must be finite.") - return SpectralStepResult(step, natural, (gradient * step).sum(), fallback | (mixing == 0)) + return vector if not mixing else _cat_parts(parts, backend, xp) def _cat_parts(parts, backend, xp): diff --git a/src/openqml/autodiff/optimizers.py b/src/openqml/autodiff/optimizers.py new file mode 100644 index 0000000..937dd7b --- /dev/null +++ b/src/openqml/autodiff/optimizers.py @@ -0,0 +1,15 @@ +"""Optional optimizer factories without importing a framework during discovery.""" + + +def hybrid_qnm(model, **options): + """Build AdamW classical groups and distance-aware QNM quantum groups.""" + try: + from ..qml.interfaces._torch_optimizer import HybridQNM + except ImportError as exc: + from ..errors import MissingDependencyError + + raise MissingDependencyError( + 'Cannot load PyTorch. Run: python -m pip install "openqml[torch]".' + ) from exc + + return HybridQNM(model, **options) diff --git a/src/openqml/compute/kernel.py b/src/openqml/compute/kernel.py index dbf2a19..baa1382 100644 --- a/src/openqml/compute/kernel.py +++ b/src/openqml/compute/kernel.py @@ -33,6 +33,10 @@ def _array_backend(value): torch = sys.modules.get("torch") if torch is not None and isinstance(value, getattr(torch, "Tensor", ())): return "torch" + # NumPy constants become Dynamo-specific array variables. Avoid both + # reflection on their dtype/module and JAX's custom instance hooks. + if isinstance(value, (np.ndarray, np.generic)): + return "numpy" if hasattr(value, "dtype") and type(value).__module__.startswith(("jax.", "jaxlib.")): jax = sys.modules.get("jax") if jax is not None and isinstance(value, (jax.Array, jax.core.Tracer)): diff --git a/src/openqml/finance/__init__.pyi b/src/openqml/finance/__init__.pyi index aa56993..598f986 100644 --- a/src/openqml/finance/__init__.pyi +++ b/src/openqml/finance/__init__.pyi @@ -6,10 +6,13 @@ from .api import price as price from .barrier import BarrierOption as BarrierOption from .budget import ExpectileBudgetResult as ExpectileBudgetResult from .budget import expectile_budget_projection as expectile_budget_projection +from .execution_risk import ExecutionRiskResult as ExecutionRiskResult +from .execution_risk import execution_risk_projection as execution_risk_projection from .expectiles import expectile as expectile from .expectiles import wasserstein_expectile as wasserstein_expectile from .models import MarketForecast as MarketForecast from .models import MarketForecastPlan as MarketForecastPlan +from .models import ScenarioDecisionResult as ScenarioDecisionResult from .models import ScenarioPlan as ScenarioPlan from .pricing import PayoffEstimate as PayoffEstimate from .pricing import PayoffOraclePlan as PayoffOraclePlan diff --git a/src/openqml/finance/_array.py b/src/openqml/finance/_array.py index 104a355..47463ca 100644 --- a/src/openqml/finance/_array.py +++ b/src/openqml/finance/_array.py @@ -4,7 +4,13 @@ import numpy as np -from ..compute.kernel import RealOps, _array_backend, floating_array, get_compute_kernel +from ..compute.kernel import ( + RealOps, + _array_backend, + _torch_compiling, + floating_array, + get_compute_kernel, +) from ..errors import ValidationError @@ -107,21 +113,35 @@ def broadcast(self, value, shape): def real(self, value, *, finite=True): if isinstance(value, (bool, complex, str, bytes)): raise ValidationError("Financial arrays must contain real numeric values.") - # Older Dynamo cannot reflect with getattr/hasattr on Tensor.dtype. - dtype = ( - value.dtype - if self.name == "torch" and isinstance(value, self.xp.Tensor) - else getattr(value, "dtype", None) - ) - if dtype is None and not isinstance(value, (int, float)): - try: - dtype = np.asarray(value).dtype - except (TypeError, ValueError, OverflowError) as exc: - raise ValidationError("Financial arrays must contain real numeric values.") from exc - if getattr(dtype, "kind", None) in {"b", "c", "O", "U", "S", "V"} or str(dtype).startswith( - ("torch.bool", "torch.complex") + if ( + self.name == "torch" + and _torch_compiling() + and isinstance(value, (np.ndarray, list, tuple)) ): - raise ValidationError("Financial arrays must contain real numeric values.") + # Preserve the original dtype before casting, while avoiding + # unsupported Dynamo reflection on a captured NumPy constant. + value = self.xp.as_tensor(value) + if self.name == "torch" and isinstance(value, self.xp.Tensor): + # Static dtype guards also work in Dynamo 2.2, which cannot + # reflect with getattr/hasattr on native dtype objects. + if value.dtype in ( + self.xp.bool, + self.xp.complex32, + self.xp.complex64, + self.xp.complex128, + ): + raise ValidationError("Financial arrays must contain real numeric values.") + else: + dtype = getattr(value, "dtype", None) + if dtype is None and not isinstance(value, (int, float)): + try: + dtype = np.asarray(value).dtype + except (TypeError, ValueError, OverflowError) as exc: + raise ValidationError( + "Financial arrays must contain real numeric values." + ) from exc + if getattr(dtype, "kind", None) in {"b", "c", "O", "U", "S", "V"}: + raise ValidationError("Financial arrays must contain real numeric values.") result = self.array(value) return ( self.check(result, self.xp.isfinite(result), "Financial arrays must be finite.") diff --git a/src/openqml/finance/_exports.py b/src/openqml/finance/_exports.py index e14016e..3ac5848 100644 --- a/src/openqml/finance/_exports.py +++ b/src/openqml/finance/_exports.py @@ -1,6 +1,8 @@ """Lazy financial API locations.""" EXPORTS = { + "ExecutionRiskResult": (".execution_risk", "ExecutionRiskResult"), + "execution_risk_projection": (".execution_risk", "execution_risk_projection"), "expectile": (".expectiles", "expectile"), "wasserstein_expectile": (".expectiles", "wasserstein_expectile"), "ExpectileBudgetResult": (".budget", "ExpectileBudgetResult"), @@ -13,6 +15,7 @@ "MarketForecast": (".models", "MarketForecast"), "MarketForecastPlan": (".models", "MarketForecastPlan"), "ScenarioPlan": (".models", "ScenarioPlan"), + "ScenarioDecisionResult": (".models", "ScenarioDecisionResult"), "PayoffEstimate": ("._results", "PayoffEstimate"), "PayoffOraclePlan": (".pricing", "PayoffOraclePlan"), "cost_aware_projection": (".projection", "cost_aware_projection"), @@ -44,6 +47,8 @@ } PUBLIC = [ + "ExecutionRiskResult", + "execution_risk_projection", "expectile", "wasserstein_expectile", "ExpectileBudgetResult", @@ -58,6 +63,7 @@ "MarketForecast", "MarketForecastPlan", "ScenarioPlan", + "ScenarioDecisionResult", "PayoffEstimate", "PayoffOraclePlan", "cost_aware_projection", diff --git a/src/openqml/finance/execution_risk.py b/src/openqml/finance/execution_risk.py new file mode 100644 index 0000000..be045e4 --- /dev/null +++ b/src/openqml/finance/execution_risk.py @@ -0,0 +1,211 @@ +"""Exact funded cash-ray risk budgets including proportional and quadratic costs.""" + +from typing import NamedTuple + +from ..errors import ConvergenceError, ValidationError +from ..ir.parameters import real +from ._array import Ops, admission, rows +from .expectiles import _expectile_root +from .risk import expected_shortfall +from .transport import _metric_values, transport_dual_norm + + +class ExecutionRiskResult(NamedTuple): + weights: object + positions: object + cash: object + scale: object + costs: object + nominal_risk: object + robust_risk: object + budget: object + + +def _upper_root(a, b, c, endpoint, ops): + """Upper feasible quadratic root, evaluated only for an active upper bound.""" + xp = ops.xp + active = a * endpoint * endpoint + b * endpoint + c > 0 + a, b, c = xp.where(active, a, 0), xp.where(active, b, 1), xp.where(active, c, -endpoint) + magnitude = ops.stop(xp.maximum(xp.maximum(xp.abs(a), xp.abs(b)), xp.abs(c))) + magnitude = xp.where(magnitude > 0, magnitude, 1) + a, b, c = a / magnitude, b / magnitude, c / magnitude + discriminant = xp.maximum(b * b - 4 * a * c, ops.array(0)) + root = xp.where(discriminant > 0, xp.sqrt(xp.where(discriminant > 0, discriminant, 1)), 0) + positive = -2 * c / xp.where(b + root > 0, b + root, 1) + negative = (-b + root) / xp.where(a > 0, 2 * a, 1) + # The rational root also covers a=0 when b>=0, retaining the correct + # derivative with respect to zero initial market impact. + return ops.clip(xp.where(b >= 0, positive, negative), 0, endpoint) + + +def execution_risk_projection( + weights, + scenarios, + *, + previous=0, + fees=0.001, + impact=0, + probabilities=None, + risk_measure="cvar", + confidence=0.95, + budget=0.02, + radius=0, + cash_return=0, + transport_metric=None, + max_memory_mb=256, + metric_iterations=128, + metric_tolerance=1e-6, +): + """Maximal cost-funded cash-ray scale meeting a CVaR or expectile budget. + + Proposal/previous positions are fractions of pre-trade NAV. Costs use + sum(fees*abs(trades)+impact*trades**2) in the same units. Returned positions, + cash and costs sum to one. Returned weights are post-cost targets accepted + by SelfFinancingPlan; they differ from pre-trade positions when costs>0. + Liquidation to cash must itself be feasible. No market order is submitted. + """ + if not isinstance(risk_measure, str) or risk_measure not in {"cvar", "expectile"}: + raise ValidationError("risk_measure must be cvar or expectile.") + confidence = real(confidence) + if not (0.5 if risk_measure == "expectile" else 0) <= confidence < 1: + raise ValidationError("Use a valid CVaR confidence or coherent expectile level.") + ops = Ops( + weights, + scenarios, + previous, + fees, + impact, + probabilities, + budget, + radius, + cash_return, + *_metric_values(transport_metric), + ) + xp = ops.xp + w, r, held, fee, quadratic = (ops.real(v) for v in (weights, scenarios, previous, fees, impact)) + if w.ndim < 1 or r.ndim < 2 or r.shape[-1] != w.shape[-1] or min(r.shape[-2:]) < 1: + raise ValidationError("Execution risk requires matching nonempty scenario and asset axes.") + r = ops.check(r, r >= -1, "Execution scenarios require arithmetic returns at least -1.") + n = w.shape[-1] + shape = ops.broadcast_shape(w.shape, held.shape, fee.shape, quadratic.shape) + if shape[-1] != n: + raise ValidationError("Execution cost and holding widths must match the assets.") + w, held, fee, quadratic = (ops.broadcast(v, shape) for v in (w, held, fee, quadratic)) + # Ops accumulates in float32 or float64; keep static IEEE spacing out of + # NumPy finfo reflection, which older Dynamo cannot trace in fullgraph. + eps = 2.0 ** (-52 if "64" in str(w.dtype) else -23) + w = ops.check( + w, + (w >= 0) & (ops.sum(w) <= 1 + 16 * n * eps)[..., None], + "Proposals must be funded and long-only.", + ) + held = ops.check( + held, + (held >= 0) & (ops.sum(held) <= 1 + 16 * n * eps)[..., None], + "Previous holdings must be funded and long-only.", + ) + w = w / ops.maximum(ops.sum(w, keepdims=True), 1) + held = held / ops.maximum(ops.sum(held, keepdims=True), 1) + fee = ops.check(fee, fee >= 0, "Trading fees must be nonnegative.") + quadratic = ops.check(quadratic, quadratic >= 0, "Market impact must be nonnegative.") + fee = ops.check( + fee, + (ops.max(fee) + 2 * ops.max(quadratic) < 1)[..., None], + "Unique financing requires max(fees)+2*max(impact)<1.", + ) + bound, rad, cash_rate = (ops.real(v) for v in (budget, radius, cash_return)) + rad = ops.check(rad, rad >= 0, "Wasserstein radii must be nonnegative.") + cash_rate = ops.check( + cash_rate, cash_rate >= -1, "Arithmetic cash returns must be at least -1." + ) + p = ops.probabilities(probabilities, r.shape[-2]) + dual = transport_dual_norm( + w, + transport_metric, + max_memory_mb=max_memory_mb, + cg_iterations=metric_iterations, + tolerance=metric_tolerance, + ) + prefix = ops.broadcast_shape( + shape[:-1], r.shape[:-2], p.shape[:-1], bound.shape, rad.shape, cash_rate.shape, dual.shape + ) + admission(rows(prefix) * (100 * n + 40 * r.shape[-2] + 4 * rows(r.shape[-2:])), max_memory_mb) + w, held, fee, quadratic = (ops.broadcast(v, prefix + (n,)) for v in (w, held, fee, quadratic)) + r = ops.broadcast(r, prefix + r.shape[-2:]) + p = ops.broadcast(p, prefix + (r.shape[-2],)) + bound, rad, cash_rate, dual = (ops.broadcast(v, prefix) for v in (bound, rad, cash_rate, dual)) + total = ops.sum(w) + losses = cash_rate[..., None] * total[..., None] - ops.einsum("...sn,...n->...s", r, w) + if risk_measure == "expectile": + nominal = _expectile_root(losses, confidence, p, 0, max_memory_mb) + robust = _expectile_root(losses, confidence, p, rad * dual, max_memory_mb) + else: + nominal = expected_shortfall(losses, confidence=confidence, probabilities=p) + robust = nominal + rad * dual / (1 - confidence) + coefficient = xp.ones_like(cash_rate) + cash_rate + a = ops.sum(quadratic * w * w) + b0 = -ops.sum(fee * w + 2 * quadratic * w * held) + c0 = ops.sum(fee * held + quadratic * held * held) + bound = ops.check( + bound, + (coefficient * c0 - cash_rate <= bound) & (c0 < 1), + "The risk budget must admit fully funded liquidation costs.", + ) + ratio = held / xp.where(w > 0, w, 1) + knots = xp.where(w > 0, ops.clip(ratio, 0, 1), 1) + flips = (w > 0) & (ratio < 1) + order = ops.argsort(knots) + knots = ops.gather(knots, order) + zero = xp.zeros_like(knots[..., :1]) + slopes = ( + ops.cat((zero, ops.cumsum(ops.gather(xp.where(flips, 2 * fee * w, 0), order))), axis=-1) + + b0[..., None] + ) + intercepts = ( + ops.cat((zero, ops.cumsum(ops.gather(xp.where(flips, -2 * fee * held, 0), order))), axis=-1) + + c0[..., None] + ) + ends = ops.cat((knots, xp.ones_like(zero)), axis=-1) + cost_at_end = a[..., None] * ends * ends + slopes * ends + intercepts + funding = total[..., None] * ends + cost_at_end + risk = robust[..., None] * ends - cash_rate[..., None] + coefficient[..., None] * cost_at_end + violation = (funding > 1) | (risk > bound[..., None]) + index = ( + violation.to(xp.int64).argmax(dim=-1, keepdim=True) + if ops.name == "torch" + else xp.argmax(violation, axis=-1)[..., None] + ) + endpoint = ops.gather(ends, index)[..., 0] + b, c = (ops.gather(v, index)[..., 0] for v in (slopes, intercepts)) + finance_root = _upper_root(a, b + total, c - 1, endpoint, ops) + risk_root = _upper_root( + coefficient * a, + coefficient * b + robust, + coefficient * c - cash_rate - bound, + endpoint, + ops, + ) + scale = xp.where(ops.sum(violation) > 0, xp.minimum(finance_root, risk_root), ops.array(1)) + positions = scale[..., None] * w + trades = positions - held + costs = ops.sum(fee * xp.abs(trades) + quadratic * trades * trades) + cash = 1 - ops.sum(positions) - costs + nominal_risk = scale * nominal - cash_rate + coefficient * costs + robust_risk = scale * robust - cash_rate + coefficient * costs + tolerance = 128 * n * eps * (1 + xp.abs(bound) + xp.abs(robust_risk)) + positions = ops.check( + positions, + ( + xp.isfinite(robust_risk) + & (robust_risk <= bound + tolerance) + & (cash >= -tolerance) + & (costs < 1) + )[..., None], + "Execution projection exceeded its risk or funding tolerance.", + error=ConvergenceError, + ) + cash = xp.maximum(cash, ops.array(0)) + post_cost = positions / (1 - costs)[..., None] + return ExecutionRiskResult( + post_cost, positions, cash, scale, costs, nominal_risk, robust_risk, bound + ) diff --git a/src/openqml/finance/models.py b/src/openqml/finance/models.py index 9a2b349..069379b 100644 --- a/src/openqml/finance/models.py +++ b/src/openqml/finance/models.py @@ -20,6 +20,13 @@ class MarketForecast(NamedTuple): median: object +class ScenarioDecisionResult(NamedTuple): + decision: object + loss: object + calibration: object + realized_return: object + + @dataclass(frozen=True) class MarketForecastPlan: input_features: int @@ -261,6 +268,62 @@ def risk(self, probabilities, *, step=None, shrinkage=0.1, variance_floor=1e-8): max_memory_mb=self.max_memory_mb, ) + def execution_decision(self, probabilities, proposal, *, step=None, **options): + """Project a proposal using this Born distribution and after-cost risk. + + Path atoms without step mean a single trade held for the whole horizon. + Supply the cash return for the same horizon. This submits no orders. + """ + from .execution_risk import execution_risk_projection + + options.setdefault("max_memory_mb", self.max_memory_mb) + return execution_risk_projection( + proposal, self.returns(step=step), probabilities=probabilities, **options + ) + + def execution_loss( + self, + probabilities, + proposal, + observations, + *, + calibration_weight=1, + decision_weight=0.1, + **options, + ): + """Native proper-score plus observed after-cost loss for end-to-end training. + + Returns per-example losses for HybridTrainer/NetworkTrainer. Observed + labels affect training loss only. Scenario risk uses the predictive + distribution and supplied proposal. Path labels describe a buy-and-hold + horizon, not a sequence of optimized future trades. + """ + if options.get("step") is not None: + raise ValidationError("execution_loss uses the full predictive and observed horizon.") + calibration_weight, decision_weight = real(calibration_weight), real(decision_weight) + if ( + min(calibration_weight, decision_weight) < 0 + or calibration_weight + decision_weight <= 0 + ): + raise ValidationError("Use nonnegative decision-loss weights with positive total.") + decision = self.execution_decision(probabilities, proposal, **options) + ops = Ops(probabilities, observations, decision.positions, decision.cash, decision.costs) + target = ops.real(observations) + target = ops.check(target, target >= -1, "Observed arithmetic returns must be at least -1.") + calibration = self.energy_score(ops.real(probabilities), target) + if self.scenarios.ndim == 3: + target = ( + (1 + target).prod(dim=-2) - 1 + if ops.name == "torch" + else ops.xp.prod(1 + target, axis=-2) - 1 + ) + cash_return = ops.real(options.get("cash_return", 0)) + realized = ( + ops.sum(decision.positions * target) + cash_return * decision.cash - decision.costs + ) + loss = calibration_weight * calibration - decision_weight * realized + return ScenarioDecisionResult(decision, loss, calibration, realized) + def expected_payoff(self, probabilities, payoffs, *, discount=1): ops = Ops(probabilities, payoffs) p = ops.probabilities(probabilities, self.num_scenarios) diff --git a/src/openqml/interop/adapters/_classical_predicate.py b/src/openqml/interop/adapters/_classical_predicate.py index 75af158..2ff2a97 100644 --- a/src/openqml/interop/adapters/_classical_predicate.py +++ b/src/openqml/interop/adapters/_classical_predicate.py @@ -98,7 +98,7 @@ class _BitVectors: def __init__(self, bdd): self.bdd = bdd - def add(self, left, right, *, subtract=False): + def add(self, left, right, *, subtract=False, with_carry=False): self.bdd.charge(len(left)) carry, result = int(subtract), [] for a, b in zip(left, right, strict=True): @@ -109,7 +109,39 @@ def add(self, left, right, *, subtract=False): carry = self.bdd.apply( "or", self.bdd.apply("and", a, b), self.bdd.apply("and", parity, carry) ) - return tuple(result) # Discard the carry: Uint arithmetic is modulo 2**width. + value = tuple(result) + return (value, carry) if with_carry else value + + def select(self, condition, high, low): + self.bdd.charge(len(high)) + return tuple( + self.bdd.apply("xor", a, self.bdd.apply("and", condition, self.bdd.apply("xor", a, b))) + for a, b in zip(low, high, strict=True) + ) + + def divide(self, left, right): + if self.bdd.truth(right) != 1: + raise conversion_error( + "CLASSICAL_DIVISOR_DOMAIN", + "Integer divisors must be symbolically proven nonzero for every classical input.", + self.bdd.location, + ) + size = len(left) + if all(bit < 2 for bit in right): + divisor = sum(bit << i for i, bit in enumerate(right)) + if divisor & (divisor - 1) == 0: + shift = divisor.bit_length() - 1 + self.bdd.charge(size) + return left[shift:] + (0,) * shift + self.bdd.charge(size * size) + remainder, divisor = (0,) * (size + 1), right + (0,) + quotient = [0] * size + for i in reversed(range(size)): + remainder = (left[i],) + remainder[:-1] + difference, carry = self.add(remainder, divisor, subtract=True, with_carry=True) + remainder = self.select(carry, difference, remainder) + quotient[i] = carry + return tuple(quotient) def multiply(self, left, right): size = len(left) @@ -124,17 +156,16 @@ def multiply(self, left, right): def shift(self, bits, amount, *, left=False, index=False): size = len(bits) - maximum = 0 - for i, bit in enumerate(amount): - if bit == 0: - continue - if i >= size.bit_length() or maximum + (1 << i) >= size: - raise conversion_error( - "CLASSICAL_INDEX_RANGE" if index else "CLASSICAL_SHIFT_RANGE", - "Every possible index/shift must be provably inside its operand width.", - self.bdd.location, - ) - maximum += 1 << i + width = max(len(amount), size.bit_length()) + extended = amount + (0,) * (width - len(amount)) + bound = tuple((size >> i) & 1 for i in range(width)) + _, greater_equal = self.add(extended, bound, subtract=True, with_carry=True) + if greater_equal != 0: + raise conversion_error( + "CLASSICAL_INDEX_RANGE" if index else "CLASSICAL_SHIFT_RANGE", + "Every possible index/shift must be symbolically proven inside its operand width.", + self.bdd.location, + ) self.bdd.charge(size * len(amount)) for i, control in enumerate(amount): if control == 0: @@ -144,12 +175,7 @@ def shift(self, bits, amount, *, left=False, index=False): if control == 1: bits = shifted else: - bits = tuple( - self.bdd.apply( - "xor", a, self.bdd.apply("and", control, self.bdd.apply("xor", a, b)) - ) - for a, b in zip(bits, shifted, strict=True) - ) + bits = self.select(control, shifted, bits) return bits @@ -239,11 +265,13 @@ def expand(bits, count): else: right = memo[id(children[1])] op = current.op.name - if op in {"ADD", "SUB", "MUL"}: + if op in {"ADD", "SUB", "MUL", "DIV"}: a, b = expand(left, size), expand(right, size) result = ( vectors.multiply(a, b) if op == "MUL" + else vectors.divide(a, b) + if op == "DIV" else vectors.add(a, b, subtract=op == "SUB") ) elif op in {"SHIFT_LEFT", "SHIFT_RIGHT"}: diff --git a/src/openqml/qml/_archive.py b/src/openqml/qml/_archive.py index b180124..7da236a 100644 --- a/src/openqml/qml/_archive.py +++ b/src/openqml/qml/_archive.py @@ -1,14 +1,32 @@ -"""Validate tensor archive declarations before allocation or native loading.""" +"""Tensor archive validation and shared atomic checkpoint writes.""" import io import math +import os +import tempfile import zipfile +from pathlib import Path import numpy as np from ..errors import ResourceError, ValidationError +def atomic_write(path, writer): + """Replace a checkpoint only after its complete writer succeeds.""" + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + handle, temporary = tempfile.mkstemp(dir=path.parent, prefix=path.name + ".") + try: + with os.fdopen(handle, "wb") as stream: + writer(stream) + os.replace(temporary, path) + finally: + if os.path.exists(temporary): + os.unlink(temporary) + return path + + def check_npz_payload(payload, maximum): from ..ir.core import integer diff --git a/src/openqml/qml/interfaces/_network_checkpoint.py b/src/openqml/qml/interfaces/_network_checkpoint.py index 84ada6b..2280d45 100644 --- a/src/openqml/qml/interfaces/_network_checkpoint.py +++ b/src/openqml/qml/interfaces/_network_checkpoint.py @@ -1,9 +1,6 @@ """Atomic, pickle-free JAX architecture/optimizer checkpoints.""" import json -import os -import tempfile -from pathlib import Path import jax import numpy as np @@ -11,7 +8,7 @@ from ...errors import ValidationError from ...ir._serialization import load_json from ...ir.core import integer -from .._archive import check_npz_payload +from .._archive import atomic_write, check_npz_payload from ..training import DepthSchedule @@ -41,21 +38,15 @@ def save_network(trainer, path): "layout": layout, "tree": str(definition), } - path = Path(path) - path.parent.mkdir(parents=True, exist_ok=True) - handle, temporary = tempfile.mkstemp(dir=path.parent, prefix=path.name + ".") - try: - with os.fdopen(handle, "wb") as stream: - np.savez_compressed( - stream, - metadata=np.asarray(json.dumps(metadata)), - **{f"leaf_{i}": np.asarray(value) for i, value in enumerate(leaves)}, - ) - os.replace(temporary, path) - finally: - if os.path.exists(temporary): - os.unlink(temporary) - return path + + def write(stream): + np.savez_compressed( + stream, + metadata=np.asarray(json.dumps(metadata)), + **{f"leaf_{i}": np.asarray(value) for i, value in enumerate(leaves)}, + ) + + return atomic_write(path, write) def load_network(trainer, path, *, max_bytes=256 * 2**20): diff --git a/src/openqml/qml/interfaces/_torch_optimizer.py b/src/openqml/qml/interfaces/_torch_optimizer.py new file mode 100644 index 0000000..f560f28 --- /dev/null +++ b/src/openqml/qml/interfaces/_torch_optimizer.py @@ -0,0 +1,296 @@ +"""AdamW/QNM optimization with bounded, backward-selected empirical geometry.""" + +import math +import threading +import weakref +from collections.abc import Mapping + +import torch + +from ...autodiff.distance_aware import DistanceAwareQNM, DistanceAwareState +from ...autodiff.geometry import quantum_metric +from ...compute.kernel import get_compute_kernel +from ...errors import ValidationError +from ...ir.parameters import real + + +def _remove_hooks(handles): + for handle in handles: + handle.remove() + handles.clear() + + +class HybridQNM(torch.optim.AdamW): + """AdamW classical groups and QFIM/momentum/polar quantum groups. + + Native unitary QuantumLayer weight vectors use empirical input geometry. + Backward-selected inputs span microbatches. DDP reduces sample-weighted + metric products, without a dense parameter matrix. Input tensors must not + mutate between forward and backward. Use state_dict for portable state. + """ + + def __init__( + self, + model, + *, + lr=1e-3, + quantum_lr=0.01, + block_shapes=None, + quantum_options=None, + weight_decay=0.01, + **adam_options, + ): + from .torch import QuantumLayer + + if not isinstance(model, torch.nn.Module): + raise ValidationError("HybridQNM requires a Torch module.") + lr, quantum_lr = real(lr), real(quantum_lr) + if lr < 0 or quantum_lr <= 0: + raise ValidationError("Use nonnegative classical and positive quantum learning rates.") + if any( + adam_options.get(n, False) + for n in ("capturable", "differentiable", "fused", "maximize") + ): + raise ValidationError("HybridQNM requires eager, minimizing optimizer steps.") + if quantum_options is not None and not isinstance(quantum_options, Mapping): + raise ValidationError("quantum_options must be a mapping.") + options = dict(quantum_options or {}) + if set(options) & {"plan", "block_shapes", "learning_rate"}: + raise ValidationError("Use block_shapes and quantum_lr for group layout and rates.") + layers = [ + (name, layer) + for name, layer in model.named_modules() + if isinstance(layer, QuantumLayer) + ] + if not layers: + raise ValidationError("HybridQNM needs at least one owned native QuantumLayer.") + if block_shapes is not None and not isinstance(block_shapes, Mapping) and len(layers) != 1: + raise ValidationError( + "For multiple quantum layers, map module names to spectral shapes." + ) + if isinstance(block_shapes, Mapping) and set(block_shapes) - {n for n, _ in layers}: + raise ValidationError("Spectral block mapping contains unknown quantum module names.") + self._quantum = [] + quantum_ids = set() + for name, layer in layers: + if layer.execution_backend != "torch" or layer.circuit.has_effects: + raise ValidationError("HybridQNM needs native unitary pure-state Torch layers.") + parameter = layer.weights + if parameter.dtype not in (torch.float32, torch.float64): + raise ValidationError("Use float32/float64 quantum master weights with AMP.") + if id(parameter) in quantum_ids: + raise ValidationError("Distinct quantum modules cannot share a weight Parameter.") + names = tuple(n for n in layer.weight_parameters if n in layer.circuit.parameters) + if not names: + continue + if parameter.ndim != 1 or parameter.shape[0] != len(layer.weight_parameters): + raise ValidationError("Quantum weight vectors must preserve circuit ownership.") + positions = tuple(layer.weight_parameters.index(n) for n in names) + plan = quantum_metric( + layer.circuit, + parameters=names, + max_memory_mb=layer.limits["max_memory_mb"], + max_qubits=layer.limits["max_qubits"], + ) + shapes = ( + block_shapes.get(name) if isinstance(block_shapes, Mapping) else block_shapes + ) or ((1, len(names)),) + geometry = DistanceAwareQNM(plan, shapes, learning_rate=quantum_lr, **options) + self._quantum.append((name, layer, parameter, positions, geometry)) + quantum_ids.add(id(parameter)) + if not self._quantum: + raise ValidationError("No active quantum weight coordinates were found.") + classical = [p for p in model.parameters() if id(p) not in quantum_ids] + groups = ( + [{"params": classical, "lr": lr, "weight_decay": weight_decay}] if classical else [] + ) + groups += [ + {"params": [p], "lr": quantum_lr, "weight_decay": 0, "openqml_quantum": name} + for name, _, p, _, _ in self._quantum + ] + super().__init__(groups, lr=lr, weight_decay=weight_decay, **adam_options) + self._distributed = isinstance(model, torch.nn.parallel.DistributedDataParallel) + self._group = model.process_group if self._distributed else None + self._lock = threading.RLock() + self._generation = 0 + self._capture = {} + self._captured_bytes = 0 + self._closed = False + handles = [] + owner = weakref.ref(self) + for name, layer, _, _, _ in self._quantum: + + def hook(module, args, kwargs, output, name=name): + optimizer = owner() + if ( + optimizer is None + or optimizer._closed + or not isinstance(output, torch.Tensor) + or not output.requires_grad + ): + return + inputs = args[0] if args else kwargs.get("inputs") + generation = optimizer._generation + handle = [] + + def capture(_gradient): + handle.pop().remove() + optimizer = owner() + if ( + optimizer is None + or optimizer._closed + or generation != optimizer._generation + ): + return + if not isinstance(inputs, torch.Tensor): + raise ValidationError("Geometry capture requires Tensor inputs.") + count = math.prod(inputs.shape[:-1]) + with optimizer._lock: + needed = count * inputs.shape[-1] * inputs.element_size() + get_compute_kernel().admit( + optimizer._captured_bytes + needed, + module.limits["max_memory_mb"], + itemsize=1, + ) + values = inputs.detach().reshape(count, inputs.shape[-1]).clone() + optimizer._capture.setdefault(name, []).append(values) + optimizer._captured_bytes += needed + + handle.append(output.register_hook(capture)) + + handles.append(layer.register_forward_hook(hook, with_kwargs=True)) + self._finalize = weakref.finalize(self, _remove_hooks, handles) + + def close(self): + self._closed = True + self._capture.clear() + self._finalize() + + def zero_grad(self, set_to_none=True): + super().zero_grad(set_to_none=set_to_none) + if hasattr(self, "_capture"): + self._capture.clear() + self._captured_bytes = 0 + self._generation += 1 + + @torch.no_grad() + def step(self, closure=None): + if self._closed: + raise ValidationError("This HybridQNM optimizer is closed.") + loss = None + if closure is not None: + with torch.enable_grad(): + loss = closure() + proposals = [] + for name, layer, p, positions, geometry in self._quantum: + present = torch.tensor(int(p.grad is not None), device=p.device, dtype=torch.int64) + gradient = torch.zeros_like(p) if p.grad is None else p.grad.detach().clone() + if self._distributed: + torch.distributed.all_reduce(present, group=self._group) + torch.distributed.all_reduce(gradient, group=self._group) + gradient = gradient / present.clamp_min(1) + if not bool(present): + continue + if not torch.isfinite(gradient).all(): + raise ValidationError("Quantum gradients must be finite before updates.") + captured = self._capture.get(name, []) + count = sum(v.shape[0] for v in captured) + total = torch.tensor(count, device=p.device, dtype=torch.int64) + if self._distributed: + torch.distributed.all_reduce(total, group=self._group) + if not bool(total > 0): + raise ValidationError( + "No geometry inputs were captured; call the owned layer's forward " + "before backward." + ) + inputs = ( + torch.cat(captured) + if count + else torch.zeros((1, len(layer.input_parameters)), dtype=p.dtype, device=p.device) + ) + bindings = { + n: inputs[:, i] + for i, n in enumerate(layer.input_parameters) + if n in layer.circuit.parameters + } + bindings.update( + (n, p.detach()[i]) + for i, n in enumerate(layer.weight_parameters) + if n in layer.circuit.parameters + ) + carry = self.state[p].get("openqml_qnm") + state = ( + None + if carry is None + else DistanceAwareState( + carry["initial"], + carry["momentum"], + carry["distance"], + torch.tensor(carry["steps"], dtype=torch.int32, device=p.device), + torch.tensor(carry["signature"], dtype=torch.int32, device=p.device), + ) + ) + group = next(g for g in self.param_groups if g.get("openqml_quantum") == name) + + def reducer(value, count=count, total=total): + value = value * (count / total.to(dtype=value.dtype)) + torch.distributed.all_reduce(value, group=self._group) + return value + + result = geometry.step( + bindings, + gradient[list(positions)], + state=state, + learning_rate=group["lr"], + metric_reducer=reducer if self._distributed else None, + ) + proposals.append( + (p, positions, result, geometry, 0 if carry is None else carry["steps"]) + ) + saved = [(p, p.grad) for _, _, p, _, _ in self._quantum] + try: + for p, _ in saved: + p.grad = None + super().step() + finally: + for p, gradient in saved: + p.grad = gradient + for p, positions, result, geometry, steps in proposals: + update = torch.zeros_like(p) + update[list(positions)] = result.step + p.sub_(update) + self.state[p]["openqml_qnm"] = { + "initial": result.state.initial_parameters, + "momentum": result.state.momentum, + "distance": result.state.max_distance, + "steps": steps + 1, + "signature": geometry._signature, + } + # AdamW's checkpoint loader expects the shared scalar counter for + # every nonempty parameter state, including custom quantum groups. + self.state[p]["step"] = torch.tensor(steps + 1, dtype=torch.float64) + self._capture.clear() + self._captured_bytes = 0 + self._generation += 1 + return loss + + def state_dict(self): + state = super().state_dict() + state["openqml_geometry"] = { + "schema": "openqml.hybrid_qnm.v1", + "groups": [(n, g._signature) for n, _, _, _, g in self._quantum], + } + return state + + def validate_state_dict(self, state_dict): + expected = { + "schema": "openqml.hybrid_qnm.v1", + "groups": [(n, g._signature) for n, _, _, _, g in self._quantum], + } + if not isinstance(state_dict, Mapping) or state_dict.get("openqml_geometry") != expected: + raise ValidationError("HybridQNM checkpoint circuit/configuration identity differs.") + + def load_state_dict(self, state_dict): + self.validate_state_dict(state_dict) + super().load_state_dict(state_dict) + self.zero_grad() diff --git a/src/openqml/qml/interfaces/_torch_training.py b/src/openqml/qml/interfaces/_torch_training.py index 8effb6e..cc7e3ae 100644 --- a/src/openqml/qml/interfaces/_torch_training.py +++ b/src/openqml/qml/interfaces/_torch_training.py @@ -1,9 +1,6 @@ """Microbatched hybrid optimization, DDP sample weighting and resumable state.""" import contextlib -import os -import tempfile -from pathlib import Path import torch @@ -11,6 +8,7 @@ from ...errors import ValidationError from ...ir.core import integer from ...ir.parameters import real +from .._archive import atomic_write def _leaves(batch): @@ -33,17 +31,7 @@ def _map(batch, function): def _atomic_save(state, path): - path = Path(path) - path.parent.mkdir(parents=True, exist_ok=True) - handle, temporary = tempfile.mkstemp(dir=path.parent, prefix=path.name + ".") - try: - with os.fdopen(handle, "wb") as stream: - torch.save(state, stream) - os.replace(temporary, path) - finally: - if os.path.exists(temporary): - os.unlink(temporary) - return path + return atomic_write(path, lambda stream: torch.save(state, stream)) class HybridTrainer: @@ -226,6 +214,9 @@ def load_state_dict(self, state, *, restore_rng=True): (state["scheduler"] is None) != (self.scheduler is None) ): raise ValidationError("Checkpoint scaler/scheduler configuration differs.") + validate = getattr(self.optimizer, "validate_state_dict", None) + if callable(validate): + validate(state["optimizer"]) rng = state if restore_rng and "rng_by_rank" in state: if self.distributed and len(state["rng_by_rank"]) != self.world_size: diff --git a/tests/test_distance_aware.py b/tests/test_distance_aware.py new file mode 100644 index 0000000..4e612c0 --- /dev/null +++ b/tests/test_distance_aware.py @@ -0,0 +1,179 @@ +"""Independent DA-QNM trajectories, resumable state and native error boundaries.""" + +import numpy as np +import pytest + +import openqml as oq + + +def _plan(): + a, b, c, p = (oq.Parameter(n) for n in ("a", "b", "c", "phase")) + circuit = oq.Circuit(2).ry(a, 0).cx(0, 1).rz(b, 1).ry(c, 1).with_global_phase(p) + return oq.quantum_metric(circuit, parameters=("a", "b", "c", "phase")) + + +def _array(value, backend): + return value.detach().cpu().numpy() if backend == "torch" else np.asarray(value) + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_stateful_trajectory_matches_independent_dense_fisher_momentum_and_polar(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + dtype = xp.float64 if backend == "torch" else xp.float32 + plan = _plan() + optimizer = oq.DistanceAwareQNM( + plan, + ((2, 2),), + damping=0.07, + momentum=0.6, + mixing=0.25, + learning_rate=0.8, + initial_radius=0.02, + distance_growth=0.3, + max_step_norm=0.07, + max_iterations=8, + alignment_guard=False, + ) + parameters = np.array([0.2, 0.3, -0.4, 0.7]) + initial, momentum = parameters.copy(), np.zeros(4) + maximum = 0.0 + state = None + + def native(theta, g, carry): + return optimizer.step( + dict(zip(plan.parameters, theta, strict=True)), g, state=carry, backend=backend + ) + + run = native if backend == "torch" else lib.jit(native) + for i in range(5): + gradient = np.array([0.3 + 0.02 * i, -0.2, 0.1 - 0.01 * i, 0.0]) + bindings = dict(zip(plan.parameters, parameters, strict=True)) + fisher = 4 * plan.matrix(bindings) + natural = np.linalg.solve(fisher + 0.07 * np.eye(4), gradient) + momentum = 0.6 * momentum + 0.4 * natural + matrix = momentum.reshape(2, 2) + u, s, vh = np.linalg.svd(matrix, full_matrices=False) + active = s > 1e-12 * s.max() + polar = (u * active[None, :]) @ vh + direction = ( + 0.75 * matrix + 0.25 * np.linalg.norm(matrix) * polar / np.sqrt(active.sum()) + ).ravel() + maximum = max(maximum, np.linalg.norm(parameters - initial)) + radius = min(0.8 * np.linalg.norm(momentum), 0.02 + 0.3 * maximum, 0.07) + step = radius * direction / (np.linalg.norm(direction) + 1e-8) + result = run(xp.asarray(parameters, dtype=dtype), xp.asarray(gradient, dtype=dtype), state) + np.testing.assert_allclose( + _array(result.natural.direction, backend), natural, rtol=3e-5, atol=2e-7 + ) + np.testing.assert_allclose(_array(result.step, backend), step, rtol=5e-5, atol=3e-7) + np.testing.assert_allclose( + _array(result.state.momentum, backend), momentum, rtol=4e-5, atol=3e-7 + ) + assert float(_array(result.state.max_distance, backend)) == pytest.approx(maximum, abs=1e-6) + assert int(result.state.steps) == i + 1 and bool(result.natural.converged) + assert np.linalg.norm(_array(result.step, backend)) <= radius + 2e-7 + parameters -= step + # A copied serialized tensor tree resumes the same trajectory. + state = oq.DistanceAwareState(*(xp.asarray(_array(v, backend)) for v in result.state)) + if backend == "torch": + assert not state.momentum.requires_grad + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_momentum_restart_zero_gradient_and_state_identity_checks(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + dtype = xp.float64 if backend == "torch" else xp.float32 + plan = _plan() + optimizer = oq.DistanceAwareQNM(plan, ((2, 2),), max_iterations=8) + theta = xp.asarray([0.2, 0.3, -0.4, 0.7], dtype=dtype) + gradient = xp.asarray([0.3, -0.2, 0.1, 0.0], dtype=dtype) + bindings = dict(zip(plan.parameters, theta, strict=True)) + first = optimizer.step(bindings, gradient, backend=backend) + reversed_state = first.state._replace(momentum=-100 * first.natural.direction) + result = optimizer.step(bindings, gradient, state=reversed_state, backend=backend) + assert bool(result.restarted) and float(_array(result.alignment, backend)) > 0 + np.testing.assert_allclose( + _array(result.state.momentum, backend), _array(result.natural.direction, backend) + ) + zero = optimizer.step(bindings, gradient * 0, state=first.state, backend=backend) + assert np.isfinite(_array(zero.step, backend)).all() and not np.any(_array(zero.step, backend)) + other = oq.DistanceAwareQNM(plan, ((2, 2),), mixing=0.2, max_iterations=8) + with pytest.raises(oq.ValidationError): + other.step(bindings, gradient, state=first.state, backend=backend) + invalid = first.state._replace( + momentum=first.state.momentum * xp.asarray(float("nan"), dtype=dtype) + ) + with pytest.raises(oq.ValidationError): + optimizer.step(bindings, gradient, state=invalid, backend=backend) + exhausted = first.state._replace(steps=xp.asarray(2_147_483_647, dtype=xp.int32)) + with pytest.raises(oq.ResourceError): + optimizer.step(bindings, gradient, state=exhausted, backend=backend) + if backend == "jax": + wrong = first.state._replace(signature=first.state.signature + 1) + raw = lib.jit(lambda s: optimizer.step(bindings, gradient, state=s, backend=backend))(wrong) + assert not np.isfinite(raw.step).all() + checked = oq.compile_compute( + lambda s: optimizer.step(bindings, gradient, state=s, backend=backend), backend=backend + ) + with pytest.raises(oq.ValidationError): + checked(wrong) + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_weighted_feature_samples_keep_shared_optimizer_coordinates(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + dtype = xp.float64 if backend == "torch" else xp.float32 + x, w = oq.Parameter("x"), oq.Parameter("w") + plan = oq.quantum_metric(oq.Circuit(1).ry(x, 0).rz(w, 0), parameters=("w",)) + optimizer = oq.DistanceAwareQNM(plan, ((1, 1),), max_iterations=4) + features = xp.asarray([0.2, 0.4, 0.7], dtype=dtype) + weights = xp.asarray([0.2, 0.3, 0.5], dtype=dtype) + gradient = xp.asarray([0.3], dtype=dtype) + result = optimizer.step( + {"x": features, "w": xp.asarray(0.3, dtype=dtype)}, + gradient, + backend=backend, + sample_weights=weights, + ) + fisher = sum(p * np.sin(v) ** 2 for p, v in zip([0.2, 0.3, 0.5], [0.2, 0.4, 0.7], strict=True)) + assert float(_array(result.natural.direction, backend)[0]) == pytest.approx( + 0.3 / (fisher + 0.01), rel=3e-5 + ) + with pytest.raises(oq.ValidationError, match="scalar"): + optimizer.step({"x": features, "w": features}, gradient, backend=backend) + + +def test_torch_compiled_state_transition_and_unchanged_previous_state(): + torch = pytest.importorskip("torch") + if tuple(int(v) for v in torch.__version__.split(".")[:2]) < (2, 4): + pytest.skip("Compiled native JVP transforms require supported Torch>=2.4.") + x = oq.Parameter("x") + plan = oq.quantum_metric(oq.Circuit(1).ry(x, 0)) + optimizer = oq.DistanceAwareQNM(plan, ((1, 1),), mixing=0, max_iterations=2) + theta = torch.tensor([0.3], dtype=torch.float64) + g = torch.tensor([0.2], dtype=torch.float64) + first = optimizer.step({"x": theta[0]}, g) + previous = first.state.momentum.clone() + run = oq.compile_compute( + lambda t, s: optimizer.step({"x": t[0]}, g, state=s), + backend="torch", + compiler="eager", + fullgraph=True, + ) + result = run(theta - first.step, first.state) + assert int(result.state.steps) == 2 + torch.testing.assert_close(first.state.momentum, previous) + + +def test_stateful_qnm_runs_on_physical_cuda(): + torch = pytest.importorskip("torch") + if not torch.cuda.is_available(): + pytest.skip("A physical CUDA device is required.") + plan = _plan() + optimizer = oq.DistanceAwareQNM(plan, ((2, 2),), max_iterations=8) + theta = torch.tensor([0.2, 0.3, -0.4, 0.7], dtype=torch.float64, device="cuda") + result = optimizer.step(dict(zip(plan.parameters, theta, strict=True)), theta * 0.1) + assert result.step.is_cuda and all(v.is_cuda for v in result.state) diff --git a/tests/test_execution_risk.py b/tests/test_execution_risk.py new file mode 100644 index 0000000..094447b --- /dev/null +++ b/tests/test_execution_risk.py @@ -0,0 +1,282 @@ +"""Independent maximality, after-cost ledger and native hypergradient checks.""" + +import numpy as np +import pytest + +import openqml as oq + +RETURNS = np.array([[-0.12, -0.03], [0.04, -0.08], [0.1, 0.02], [-0.05, 0.01]]) +PROBABILITIES = np.array([0.2, 0.3, 0.4, 0.1]) + + +def _risk(loss, p, level, measure, margin): + p = p / p.sum() + if measure == "cvar": + order = np.argsort(loss)[::-1] + mass = p[order] + admitted = np.minimum(mass, np.maximum(1 - level - np.cumsum(mass) + mass, 0)) + return np.dot(admitted, loss[order]) / (1 - level) + margin / (1 - level) + low, high = min(loss) - 1, max(loss) + 1 + level * margin / (1 - level) + for _ in range(120): + middle = (low + high) / 2 + residual = ( + level * np.dot(p, np.maximum(loss - middle, 0)) + - (1 - level) * np.dot(p, np.maximum(middle - loss, 0)) + + level * margin + ) + if residual > 0: + low = middle + else: + high = middle + return (low + high) / 2 + + +def _reference(scale, w, previous, r, p, fees, impact, cash_return, level, measure, radius, metric): + positions = scale * w + trades = positions - previous + costs = np.sum(fees * np.abs(trades) + impact * trades**2) + cash = 1 - positions.sum() - costs + losses = costs - r @ positions - cash_return * cash + margin = radius * np.sqrt(positions @ np.linalg.solve(metric, positions)) + return _risk(losses, p, level, measure, margin), cash, costs + + +@pytest.mark.parametrize( + "measure,level", [("cvar", 0), ("cvar", 0.8), ("expectile", 0.5), ("expectile", 0.9)] +) +@pytest.mark.parametrize("cash_return", [0, 0.003]) +def test_sorted_quadratic_projection_matches_independent_feasible_prefix( + measure, level, cash_return +): + f = oq.finance + w, previous = np.array([0.4, 0.5]), np.array([0.3, 0.2]) + fees, impact = np.array([0.001, 0.002]), np.array([0.02, 0.03]) + diagonal, factors = np.array([0.7, 1.2]), np.array([[0.2, -0.3]]) + metric = np.diag(diagonal) + factors.T @ factors + options = dict( + previous=previous, + fees=fees, + impact=impact, + probabilities=PROBABILITIES, + risk_measure=measure, + confidence=level, + budget=0.015, + radius=0.001, + cash_return=cash_return, + transport_metric=f.WassersteinMetric(diagonal, factors), + ) + result = f.execution_risk_projection(w, RETURNS, **options) + + def evaluate(scale): + return _reference( + scale, + w, + previous, + RETURNS, + PROBABILITIES, + fees, + impact, + cash_return, + level, + measure, + 0.001, + metric, + ) + + if evaluate(1)[0] <= 0.015 and evaluate(1)[1] >= 0: + expected = 1.0 + else: + low, high = 0.0, 1.0 + for _ in range(90): + midpoint = (low + high) / 2 + risk, cash, _ = evaluate(midpoint) + if risk <= 0.015 and cash >= 0: + low = midpoint + else: + high = midpoint + expected = (low + high) / 2 + assert result.scale == pytest.approx(expected, abs=4e-13) + risk, cash, cost = evaluate(float(result.scale)) + assert result.robust_risk == pytest.approx(risk, abs=2e-14) + assert result.cash == pytest.approx(cash, abs=2e-14) + assert result.costs == pytest.approx(cost, abs=2e-14) + assert risk <= 0.015 + 2e-14 and cash >= -2e-14 + assert result.positions.sum() + result.cash + result.costs == pytest.approx(1, abs=2e-14) + np.testing.assert_allclose(result.weights * (1 - result.costs), result.positions, atol=2e-14) + + +def test_funding_bound_context_batches_and_zero_proposals(): + f = oq.finance + w = np.array([[0.4, 0.6], [0.0, 0.9]]) + result = f.execution_risk_projection(w, np.zeros((3, 2)), previous=0, fees=0.01, budget=0.5) + expected = np.minimum(1, 1 / ((1 + 0.01) * w.sum(-1))) + np.testing.assert_allclose(result.scale, expected, atol=2e-15) + np.testing.assert_allclose(result.cash, 1 - expected * w.sum(-1) * (1 + 0.01), atol=2e-15) + assert ( + f.execution_risk_projection( + [0.0, 0.0], RETURNS, previous=[0.2, 0.3], budget=0.02 + ).positions.sum() + == 0 + ) + + +@pytest.mark.parametrize("measure,level", [("cvar", 0.8), ("expectile", 0.9)]) +def test_returned_post_cost_targets_reproduce_self_financing_ledger(measure, level): + f = oq.finance + previous = np.array([0.3, 0.2]) + fee, impact = np.array([0.001, 0.002]), np.array([0.02, 0.03]) + result = f.execution_risk_projection( + [0.4, 0.5], + RETURNS, + previous=previous, + fees=fee, + impact=impact, + risk_measure=measure, + confidence=level, + budget=0.015, + cash_return=0.003, + ) + count = len(RETURNS) + target = np.broadcast_to(result.weights, (count, 1, 2)) + state = f.TradingState( + np.broadcast_to(previous, (count, 2)), + np.full(count, 1 - previous.sum()), + target, + np.ones((count, 1), dtype=bool), + ) + ledger = f.SelfFinancingPlan(fees=fee, impact=impact).rollout( + RETURNS[:, None, :], target, state=state, cash_returns=0.003 + ) + np.testing.assert_allclose(ledger.costs[:, 0], result.costs, atol=3e-14) + loss = 1 - ledger.wealth[:, 0] + nominal = _risk(loss, np.ones(count) / count, level, measure, 0) + assert nominal == pytest.approx(float(result.nominal_risk), abs=4e-14) + + +def _objective(values, backend): + f = oq.finance + state = oq.statevector( + oq.Circuit(1).ry(oq.Parameter("theta"), 0), {"theta": values[0]}, backend=backend + ) + p = state.real**2 + state.imag**2 + result = f.execution_risk_projection( + [0.4, 0.5], + RETURNS[:2], + previous=[0.3, 0.2], + fees=values[1:3], + impact=values[3:5], + probabilities=p, + risk_measure="expectile", + confidence=0.8, + radius=values[5], + budget=0.018, + ) + return -0.03 * result.positions[0] + 0.01 * result.positions[1] + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_quantum_probabilities_fees_impact_and_radius_keep_native_derivatives(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + dtype = xp.float64 if backend == "torch" else xp.float32 + base = np.array([1.4, 0.005, 0.003, 0.04, 0.02, 0.001]) + values = xp.asarray(base, dtype=dtype) + if backend == "torch": + values.requires_grad_() + assert lib.autograd.gradcheck(lambda v: _objective(v, backend), (values,)) + assert lib.autograd.gradgradcheck(lambda v: _objective(v, backend), (values,)) + gradient = lib.autograd.grad(_objective(values, backend), values)[0].detach().numpy() + else: + gradient = np.asarray(lib.jit(lib.grad(lambda v: _objective(v, backend)))(values)) + + def reference(v): + p = np.array([np.cos(v[0] / 2) ** 2, np.sin(v[0] / 2) ** 2]) + w, previous = np.array([0.4, 0.5]), np.array([0.3, 0.2]) + low, high = 0.0, 1.0 + for _ in range(90): + middle = (low + high) / 2 + risk, cash, _ = _reference( + middle, + w, + previous, + RETURNS[:2], + p, + v[1:3], + v[3:5], + 0, + 0.8, + "expectile", + v[5], + np.eye(2), + ) + if risk <= 0.018 and cash >= 0: + low = middle + else: + high = middle + return (-0.03 * w[0] + 0.01 * w[1]) * (low + high) / 2 + + expected = [] + for i in range(6): + delta = np.eye(6)[i] * 1e-6 + expected.append((reference(base + delta) - reference(base - delta)) / 2e-6) + assert np.isfinite(gradient).all() and (np.abs(gradient) > 1e-8).all() + np.testing.assert_allclose(gradient, expected, rtol=3e-3, atol=3e-7) + + +def test_zero_initial_impact_has_a_nonzero_correct_one_sided_derivative(): + torch = pytest.importorskip("torch") + impact = torch.tensor(0.0, dtype=torch.float64, requires_grad=True) + function = oq.finance.execution_risk_projection + + def objective(value): + return function( + [0.4, 0.5], + RETURNS, + previous=[0.3, 0.2], + fees=0.001, + impact=value, + budget=0.015, + confidence=0.8, + ).positions.sum() + + gradient = torch.autograd.grad(objective(impact), impact)[0] + difference = (objective(torch.tensor(1e-6, dtype=impact.dtype)) - objective(impact)) / 1e-6 + assert abs(float(gradient)) > 1e-5 + torch.testing.assert_close(gradient, difference, rtol=2e-4, atol=1e-7) + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_compiled_funding_checks_survive_native_execution(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + project = oq.finance.execution_risk_projection + run = oq.compile_compute( + lambda w: project(w, RETURNS, previous=[0.3, 0.2], budget=0.015), + backend=backend, + **({"compiler": "eager", "fullgraph": True} if backend == "torch" else {}), + ) + result = run(xp.asarray([0.4, 0.5])) + assert float(result.robust_risk) <= 0.015 + 1e-6 + with pytest.raises(oq.ValidationError): + run(xp.asarray([-0.4, 0.5])) + + +@pytest.mark.parametrize( + "options", + [ + {"fees": -1}, + {"impact": -0.1}, + {"fees": 0.5, "impact": 0.25}, + {"previous": [1.0, 1.0]}, + {"budget": 0.00001}, + {"radius": -1}, + {"risk_measure": "bad"}, + {"cash_return": -2}, + {"max_memory_mb": 1e-9}, + ], +) +def test_invalid_execution_domains_and_infeasible_liquidation_fail(options): + kwargs = dict(previous=[0.3, 0.2], fees=0.01) + kwargs.update(options) + with pytest.raises((oq.ValidationError, oq.ResourceError)): + oq.finance.execution_risk_projection([0.4, 0.5], RETURNS, **kwargs) diff --git a/tests/test_hybrid_qnm.py b/tests/test_hybrid_qnm.py new file mode 100644 index 0000000..6805997 --- /dev/null +++ b/tests/test_hybrid_qnm.py @@ -0,0 +1,192 @@ +"""End-to-end hybrid optimization, microbatch invariance and checkpoint replay.""" + +import copy +import io +from datetime import timedelta + +import numpy as np +import pytest + +import openqml as oq + + +def _model(): + import torch + + from openqml.qml.interfaces.torch import QuantumLayer + + torch.manual_seed(42) + + class Model(torch.nn.Module): + def __init__(self): + super().__init__() + x, y, a, b, c, d = (oq.Parameter(n) for n in ("x", "y", "a", "b", "c", "d")) + circuit = oq.Circuit(2).ry(x, 0).ry(y, 1).ry(a, 0).cx(0, 1).rz(b, 1).rx(c, 0).ry(d, 1) + self.encoder = torch.nn.Linear(2, 2, dtype=torch.float64) + self.q = QuantumLayer( + circuit, ("x", "y"), ("a", "b", "c", "d"), dtype=torch.float64, device="cpu" + ) + self.head = torch.nn.Linear(2, 1, dtype=torch.float64) + + def forward(self, x): + return self.head(self.q(self.encoder(x))) + + return Model() + + +def _trainer(model, microbatch): + from openqml.qml.interfaces.torch import HybridTrainer + + optimizer = oq.hybrid_qnm( + model, + quantum_lr=0.03, + block_shapes={"q" if not hasattr(model, "module") else "module.q": ((2, 2),)}, + quantum_options={"damping": 0.1, "tolerance": 1e-9, "max_iterations": 12}, + ) + return HybridTrainer(model, optimizer, microbatch_size=microbatch, max_grad_norm=None) + + +def _loss(model, batch): + return (model(batch[0]) - batch[1]).square().mean() + + +def test_quantum_and_classical_parameters_train_with_microbatch_invariant_geometry(): + torch = pytest.importorskip("torch") + first, second = _model(), _model() + a, b = _trainer(first, 2), _trainer(second, 5) + x = torch.arange(10, dtype=torch.float64).reshape(5, 2) / 7 + y = x.sum(-1, keepdim=True).sin() + original = [p.detach().clone() for p in first.parameters()] + for _ in range(3): + result_a, result_b = a.step((x, y), _loss), b.step((x, y), _loss) + assert not result_a["skipped"] and not result_b["skipped"] + for p, q in zip(first.parameters(), second.parameters(), strict=True): + torch.testing.assert_close(p, q, rtol=1e-9, atol=1e-11) + assert all(not torch.equal(p, old) for p, old in zip(first.parameters(), original, strict=True)) + assert a.optimizer.state[first.q.weights]["openqml_qnm"]["steps"] == 3 + assert not a.optimizer._capture and a.optimizer._captured_bytes == 0 + a.optimizer.close() + b.optimizer.close() + + +def test_full_training_checkpoint_replays_quantum_momentum_and_rejects_wrong_identity(): + torch = pytest.importorskip("torch") + model = _model() + trainer = _trainer(model, 2) + x = torch.arange(10, dtype=torch.float64).reshape(5, 2) / 7 + batch = (x, x.sum(-1, keepdim=True).sin()) + trainer.step(batch, _loss) + stream = io.BytesIO() + torch.save(trainer.state_dict(), stream) + trainer.step(batch, _loss) + expected = [p.detach().clone() for p in model.parameters()] + restored = _model() + other = _trainer(restored, 2) + stream.seek(0) + state = torch.load(stream, weights_only=False) + other.load_state_dict(state) + other.step(batch, _loss) + for p, q in zip(restored.parameters(), expected, strict=True): + torch.testing.assert_close(p, q, rtol=1e-12, atol=1e-13) + bad = copy.deepcopy(state) + bad["optimizer"]["openqml_geometry"]["schema"] = "wrong" + before = [p.detach().clone() for p in restored.parameters()] + with pytest.raises(oq.ValidationError): + other.load_state_dict(bad) + assert all(torch.equal(p, q) for p, q in zip(restored.parameters(), before, strict=True)) + trainer.optimizer.close() + other.optimizer.close() + + +def test_unused_forward_and_zero_learning_rate_do_not_corrupt_context_or_weights(): + torch = pytest.importorskip("torch") + model = _model() + trainer = _trainer(model, 5) + model(torch.ones((7, 2), dtype=torch.float64)) + assert not trainer.optimizer._capture + for group in trainer.optimizer.param_groups: + group["lr"] = 0 + x = torch.ones((5, 2), dtype=torch.float64) + before = [p.detach().clone() for p in model.parameters()] + trainer.step((x, x[:, :1] * 0.2), _loss) + assert all(torch.equal(p, q) for p, q in zip(model.parameters(), before, strict=True)) + assert trainer.optimizer.state[model.q.weights]["openqml_qnm"]["steps"] == 1 + trainer.optimizer.close() + with pytest.raises(oq.ValidationError, match="closed"): + trainer.optimizer.step() + + +def _worker(rank, path, output): + import torch + import torch.distributed as dist + from torch.nn.parallel import DistributedDataParallel + + dist.init_process_group( + "gloo", init_method="file://" + path, rank=rank, world_size=2, timeout=timedelta(seconds=90) + ) + try: + model = _model() + ddp = DistributedDataParallel(model) + trainer = _trainer(ddp, 1) + x = torch.arange(10, dtype=torch.float64).reshape(5, 2) / 7 + y = x.sum(-1, keepdim=True).sin() + indices = slice(0, 2) if rank == 0 else slice(2, 5) + for _ in range(2): + trainer.step((x[indices], y[indices]), _loss) + if rank == 0: + torch.save(model.state_dict(), output) + trainer.optimizer.close() + finally: + dist.destroy_process_group() + + +def test_distributed_hybrid_qnm_global_metric_matches_unequal_rank_reference(tmp_path): + torch = pytest.importorskip("torch") + if not torch.distributed.is_available() or not torch.distributed.is_gloo_available(): + pytest.skip("Distributed Torch is unavailable.") + import torch.multiprocessing as mp + + output = str(tmp_path / "rank-zero.pt") + mp.spawn(_worker, args=(str(tmp_path / "rendezvous"), output), nprocs=2, join=True) + model = _model() + trainer = _trainer(model, 5) + x = torch.arange(10, dtype=torch.float64).reshape(5, 2) / 7 + for _ in range(2): + trainer.step((x, x.sum(-1, keepdim=True).sin()), _loss) + expected = torch.load(output, weights_only=False) + for name, value in model.state_dict().items(): + if isinstance(value, torch.Tensor): + torch.testing.assert_close(value, expected[name], rtol=1e-8, atol=2e-10) + trainer.optimizer.close() + + +@pytest.mark.parametrize("backend", ["torch", "jax"]) +def test_scenario_execution_objective_trains_probabilities_and_proposals(backend): + lib = pytest.importorskip(backend) + xp = lib if backend == "torch" else lib.numpy + dtype = xp.float64 if backend == "torch" else xp.float32 + plan = oq.finance.ScenarioPlan([[-0.12, -0.03], [0.04, -0.08], [0.1, 0.02], [-0.05, 0.01]]) + value = xp.asarray([0.3, -0.4, 0.2, 0.1, 0.4, 0.5], dtype=dtype) + + def objective(v): + p = xp.exp(v[:4]) + p = p / p.sum() + result = plan.execution_loss( + p, + v[4:], + xp.asarray([0.02, -0.01], dtype=dtype), + previous=[0.3, 0.2], + budget=0.02, + confidence=0.7, + radius=0.001, + impact=0.01, + ) + return result.loss + + if backend == "torch": + value.requires_grad_() + assert lib.autograd.gradcheck(objective, (value,)) + gradient = lib.autograd.grad(objective(value), value)[0].detach().numpy() + else: + gradient = np.asarray(lib.jit(lib.grad(objective))(value)) + assert np.isfinite(gradient).all() and (np.abs(gradient) > 1e-8).all() diff --git a/tests/test_integer_division.py b/tests/test_integer_division.py new file mode 100644 index 0000000..374bec8 --- /dev/null +++ b/tests/test_integer_division.py @@ -0,0 +1,92 @@ +"""Bounded symbolic division and correlation-aware selector range proofs.""" + +import pytest + +import openqml as oq + + +def _sdk(): + qiskit = pytest.importorskip("qiskit") + from qiskit.circuit.classical import expr, types + + if not hasattr(expr, "div"): + pytest.skip("This Qiskit predates typed integer arithmetic.") + return qiskit, expr, types + + +def test_variable_integer_division_is_exact_for_every_three_bit_input(): + qiskit, expr, types = _sdk() + for a in range(8): + for b in range(4): + ar, br = qiskit.ClassicalRegister(3, "a"), qiskit.ClassicalRegister(2, "b") + source = qiskit.QuantumCircuit(qiskit.QuantumRegister(6), ar, br) + for i in range(5): + if ((a | (b << 3)) >> i) & 1: + source.x(i) + source.measure(i, i) + positive = expr.add(expr.cast(br, types.Uint(3)), 1) + with source.if_test(expr.equal(expr.div(ar, positive), 2)): + source.x(5) + source.measure(5, 0) + ir = oq.convert(source, target="ir").value + expected = -1 if a // (b + 1) == 2 else 1 + observable = oq.PauliString("IIIIIZ") + assert oq.expectation(ir, observable) == pytest.approx(expected, abs=2e-12) + qasm = oq.convert(ir, target="openqasm3").value + back = oq.convert(qasm, source="openqasm3", target="ir").value + assert oq.expectation(back, observable) == pytest.approx(expected, abs=2e-12) + + +@pytest.mark.parametrize("divisor", [1, 2, 3, 4, 5, 7]) +def test_constant_divisors_preserve_unsigned_quotients(divisor): + qiskit, expr, _ = _sdk() + for a in range(8): + source = qiskit.QuantumCircuit(4, 3) + for i in range(3): + if (a >> i) & 1: + source.x(i) + source.measure(i, i) + with source.if_test(expr.equal(expr.div(source.cregs[0], divisor), 1)): + source.x(3) + ir = oq.convert(source, target="ir").value + assert oq.expectation(ir, oq.PauliString("IIIZ")) == pytest.approx( + -1 if a // divisor == 1 else 1 + ) + + +def test_correlated_selector_bits_prove_a_range_that_independent_bit_bounds_miss(): + qiskit, expr, types = _sdk() + for data in range(8): + for selector in range(2): + ar, br = qiskit.ClassicalRegister(3, "data"), qiskit.ClassicalRegister(1, "selector") + source = qiskit.QuantumCircuit(qiskit.QuantumRegister(5), ar, br) + for i in range(4): + if ((data | (selector << 3)) >> i) & 1: + source.x(i) + source.measure(i, i) + low = expr.cast(br[0], types.Uint(2)) + high = expr.shift_left(expr.cast(expr.logic_not(br[0]), types.Uint(2)), 1) + index = expr.bit_or(low, high) # Exactly 1 or 2, never 0 or 3. + with source.if_test(expr.index(ar, index)): + source.x(4) + ir = oq.convert(source, target="ir").value + bit = 1 if selector else 2 + assert oq.expectation(ir, oq.PauliString("IIIIZ")) == pytest.approx( + -1 if (data >> bit) & 1 else 1 + ) + + +def test_undefined_divisors_and_quadratic_work_retain_explicit_diagnostics(): + qiskit, expr, types = _sdk() + source = qiskit.QuantumCircuit(1, 3) + with source.if_test(expr.equal(expr.div(source.cregs[0], source.cregs[0]), 1)): + source.x(0) + with pytest.raises(oq.ConversionError, match="DIVISOR"): + oq.convert(source, target="ir") + wide = qiskit.QuantumCircuit(1) + with wide.if_test( + expr.equal(expr.div(expr.Value(0, types.Uint(64)), expr.Value(3, types.Uint(64))), 0) + ): + wide.x(0) + with pytest.raises(oq.ConversionError, match="LIMIT"): + oq.convert(wide, target="ir", max_operations=1000) diff --git a/tests/test_model_checkpoint.py b/tests/test_model_checkpoint.py index 3392a99..212b8c6 100644 --- a/tests/test_model_checkpoint.py +++ b/tests/test_model_checkpoint.py @@ -86,6 +86,24 @@ def broken(*_): np.testing.assert_array_equal(model.predict(x), loaded.predict(x)) +def test_interrupted_checkpoint_writer_keeps_old_bytes_and_removes_partial_file(tmp_path): + from openqml.qml._archive import atomic_write + + path = tmp_path / "training.state" + path.write_bytes(b"complete old checkpoint") + + def interrupted(stream): + stream.write(b"partial replacement") + raise OSError("interrupted during serialization") + + with pytest.raises(OSError, match="during serialization"): + atomic_write(path, interrupted) + assert path.read_bytes() == b"complete old checkpoint" + assert list(tmp_path.iterdir()) == [path] + atomic_write(path, lambda stream: stream.write(b"complete replacement")) + assert path.read_bytes() == b"complete replacement" + + def test_checkpoint_rejects_unfitted_malformed_oversized_and_traversal_archives(tmp_path): with pytest.raises(oq.ValidationError, match="fit"): oq.Model().save(tmp_path / "none")