Circuit Module

This section documents the quforge.circuit module which handles the creation and manipulation of quantum circuits.

class quforge.circuit.Circuit(dim=2, wires=1, device='cpu', sparse=False)[source]

Bases: Module

Quantum Circuit for qudits.

The Circuit class allows users to dynamically add various quantum gates to construct a quantum circuit for qudit systems. It supports a wide range of gates, including single-qudit, multi-qudit, and custom gates. The circuit is represented as a sequence of quantum operations (gates) that act on qudit states.

Arguments:
dim (int or list of int): The dimension of the qudits. If an integer, all qudits are assumed

to have that dimension; if a list is provided, each element specifies the dimension of the corresponding qudit. wires (int): The total number of qudits (wires) in the circuit (used when dim is an integer). If dim is a list, wires is taken as the length of that list.

device (str): The device to perform the computations on. Default is ‘cpu’. sparse (bool): Whether to use sparse matrix representations for the gates. Default is False.

Attributes:

dim (int or list of int): The dimension(s) of the qudits. wires (int): The number of qudits in the circuit. device (str): The device for computations (‘cpu’ or ‘cuda’). circuit (nn.Sequential): A sequential container for holding the quantum gates. sparse (bool): Whether to use sparse matrices in the gates.

Methods:

add(module, **kwargs): Dynamically add a gate module to the circuit. add_gate(gate, **kwargs): Add a specific gate instance to the circuit. H(**kwargs): Add a Hadamard gate to the circuit. X(**kwargs): Add a Pauli-X gate to the circuit. Y(**kwargs): Add a Pauli-Y gate to the circuit. Z(**kwargs): Add a Pauli-Z gate to the circuit. RX(**kwargs): Add a rotation-X gate to the circuit. RY(**kwargs): Add a rotation-Y gate to the circuit. RZ(**kwargs): Add a rotation-Z gate to the circuit. CNOT(**kwargs): Add a controlled-NOT gate to the circuit. SWAP(**kwargs): Add a SWAP gate to the circuit. CZ(**kwargs): Add a controlled-Z gate to the circuit. CCNOT(**kwargs): Add a Toffoli (CCNOT) gate to the circuit. MCX(**kwargs): Add a multi-controlled-X gate to the circuit. CRX(**kwargs): Add a controlled rotation-X gate to the circuit. CRY(**kwargs): Add a controlled rotation-Y gate to the circuit. CRZ(**kwargs): Add a controlled rotation-Z gate to the circuit. U(**kwargs): Add a universal gate to the circuit. CU(**kwargs): Add a controlled-universal gate to the circuit.

Example:
>>> import quforge.quforge as qf
>>> circuit = qf.Circuit(dim=[2,3,2], wires=3, device='cpu')  # Multidimensional: qudit0:2, qudit1:3, qudit2:2
>>> circuit.H(index=[0])
>>> circuit.CNOT(index=[0, 1])
>>> state = qf.State('0-1-0', dim=[2,3,2])
>>> result = circuit(state)
>>> print(result)
CCNOT(**kwargs)[source]
CNOT(**kwargs)[source]
CRX(**kwargs)[source]
CRY(**kwargs)[source]
CRZ(**kwargs)[source]
CU(**kwargs)[source]
CZ(**kwargs)[source]
H(**kwargs)[source]
MCX(**kwargs)[source]
RX(**kwargs)[source]
RY(**kwargs)[source]
RZ(**kwargs)[source]
SWAP(**kwargs)[source]
U(**kwargs)[source]
X(**kwargs)[source]
Y(**kwargs)[source]
Z(**kwargs)[source]
add(module, **kwargs)[source]

Dynamically add a gate module to the circuit.

Parameters:
  • module – The gate module to add.

  • **kwargs – Additional arguments for the gate.

add_gate(gate, **kwargs)[source]

Add a pre-instantiated gate to the circuit.

Parameters:
  • gate – The gate instance to add.

  • **kwargs – Additional arguments for the gate.

forward(x)[source]

Apply the circuit to the input qudit state.

Parameters:

x (torch.Tensor) – The input qudit state (a column vector) whose dimension equals the product of the individual qudit dimensions.

Returns:

The resulting state after applying the circuit.

Return type:

torch.Tensor

training: bool