uniqc.torch_adapter.tq_quantum_layer module

TorchQuantumLayer: nn.Module with native PyTorch autograd via TorchQuantum.

Unlike QuantumLayer (parameter-shift rule), this layer gets gradients for free through TorchQuantum’s differentiable statevector simulation.

class uniqc.torch_adapter.tq_quantum_layer.TorchQuantumLayer(circuit_builder, n_qubits, n_params, hamiltonian, init_params=None, device='cpu')[source]

Bases: Module

PyTorch layer using TorchQuantum for native autograd.

Takes a circuit builder callable that constructs opcodes from a parameter tensor. Gradients propagate through PyTorch autograd natively — no parameter-shift rule needed.

Parameters:
  • circuit_builder – Callable(params_tensor) -> (opcode_list, n_qubits, param_overrides). Constructs the circuit with tensor parameters.

  • n_qubits – Number of qubits.

  • n_params – Number of trainable parameters.

  • hamiltonian – List of (pauli_string, coefficient) for expectation.

  • init_params – Initial parameter values (optional).

  • device – “cpu” or “cuda”.

extra_repr()[source]

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x=None)[source]

Execute circuit and return expectation value.

Parameters:

x – Optional input tensor (for data encoding circuits).

Returns:

Differentiable scalar tensor with expectation value.