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 Circuit whose opcode_list defines the unitary.

  • hamiltonian – List of (pauli_string, coefficient) tuples. Each pauli_string is a sequence of I, X, Y, Z characters (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 opcode params field. If None, falls back to circuit.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 (with requires_grad when any tensor in param_map requires gradients).