uniqc.torch_adapter.expectation module¶
Backend-agnostic differentiable expectation values for quantum circuits.
Public API:
expectation(circuit, hamiltonian, param_map=None, backend="virtual") -> Tensor
The backend parameter selects the execution engine:
"virtual"(default) — native PyTorch statevector simulation, fully differentiable, no external dependencies beyond torch."torchquantum"— delegates to the TorchQuantum-based simulator (optional).
Additional backends (density matrix, MPS, real hardware) can be registered in future versions.
- uniqc.torch_adapter.expectation.expectation(circuit, hamiltonian, param_map=None, backend='virtual')[source]¶
Compute the differentiable expectation value ⟨ψ|H|ψ⟩.
- Parameters:
circuit – A
Circuitwhoseopcode_listdefines the unitary.hamiltonian – List of
(pauli_string, coefficient)tuples. Each pauli_string is a sequence ofI,X,Y,Zcharacters (e.g."ZII","XX"). The length must match the number of qubits acted on by the circuit.param_map – Optional
{opcode_index: torch.Tensor}mapping that overrides the float values stored in the opcodeparamsfield. If None, falls back tocircuit.param_map.backend – Execution backend.
"virtual"(default) uses a native PyTorch statevector simulation."torchquantum"delegates to the TorchQuantum-based simulator.
- Returns:
A scalar
torch.Tensor(withrequires_gradwhen any tensor in param_map requires gradients).