operation#

Base classes for quantum operations

Classes

Channel([inputs, name, nqubit, wires, ...])

A base class for quantum channels.

Gate([name, nqubit, wires, controls, ...])

A base class for quantum gates.

GateQPD(bases, coeffs[, label, name, ...])

A base class for quasiprobability-decomposition gates.

Layer([name, nqubit, wires, den_mat, tsr_mode])

A base class for quantum layers.

MeasureQPD([nqubit, wires])

A operation for denoting a QPD measurement location.

Operation([name, nqubit, wires, den_mat, ...])

A base class for quantum operations.

class Channel(inputs: Any = None, name: str | None = None, nqubit: int = 1, wires: int | list[int] | None = None, tsr_mode: bool = False, requires_grad: bool = False)[source]#

Bases: Operation

A base class for quantum channels.

Parameters:
  • inputs (Any) – The parameter of the channel. Default: None

  • name (str | None) – The name of the channel. Default: None

  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • tsr_mode (bool) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: False

  • requires_grad (bool) – Whether the parameter is nn.Parameter or buffer. Default: False (which means buffer)

extra_repr() str[source]#
forward(x: Tensor) Tensor[source]#

Perform a forward pass.

get_matrix(theta: Any) Tensor[source]#

Update the local Kraus matrices acting on density matrices.

init_para(inputs: Any = None) None[source]#

Initialize the parameters.

inputs_to_tensor(inputs: Any = None) Tensor[source]#

Convert inputs to torch.Tensor.

op_den_mat(x: Tensor) Tensor[source]#

Perform a forward pass for density matrices.

property prob#

The error probability.

update_matrix() Tensor[source]#

Update the local Kraus matrices acting on density matrices.

class Gate(name: str | None = None, nqubit: int = 1, wires: int | list[int] | None = None, controls: int | list[int] | None = None, condition: bool = False, den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: Operation

A base class for quantum gates.

Parameters:
  • name (str | None) – The name of the gate. Default: None

  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • controls (int | list[int] | None) – The indices of the control qubits. Default: None

  • condition (bool) – Whether to use controls as conditional measurement. Default: False

  • den_mat (bool) – Whether the quantum operation acts on density matrices or state vectors. Default: False (which means state vectors)

  • tsr_mode (bool) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: False

extra_repr() str[source]#
forward(x: Tensor | MatrixProductState | DistributedQubitState) Tensor | MatrixProductState | DistributedQubitState[source]#

Perform a forward pass.

get_derivative(inputs: Any) Tensor[source]#

Get the derivative of the local unitary matrix.

get_matrix(inputs: Any) Tensor[source]#

Get the local unitary matrix.

get_mpo() tuple[list[Tensor], int][source]#

Convert gate to MPO form with identities at empty sites.

Note

If sites are not adjacent, insert identities in the middle, i.e.,

>>>      |       |            |   |   |
>>>    --A---x---B--   ->   --A---I---B--
>>>      |       |            |   |   |

where

>>>         a
>>>         |
>>>    --i--I--j--
>>>         |
>>>         b

means \(\delta_{i,j} \delta_{a,b}\)

inverse() Gate[source]#

Get the inversed gate.

op_den_mat(x: Tensor) Tensor[source]#

Perform a forward pass for density matrices.

op_den_mat_base(x: Tensor, matrix: Tensor) Tensor[source]#

Perform a forward pass of a gate for density matrices.

op_den_mat_control(x: Tensor, matrix: Tensor) Tensor[source]#

Perform a forward pass of a controlled gate for density matrices.

op_dist_state(x: DistributedQubitState) DistributedQubitState[source]#

Perform a forward pass of a gate for a distributed state vector.

op_mps(mps: MatrixProductState) MatrixProductState[source]#

Perform a forward pass for the MatrixProductState.

op_state(x: Tensor) Tensor[source]#

Perform a forward pass for state vectors.

op_state_base(x: Tensor, matrix: Tensor) Tensor[source]#

Perform a forward pass of a gate for state vectors.

op_state_control(x: Tensor, matrix: Tensor) Tensor[source]#

Perform a forward pass of a controlled gate for state vectors.

qpd(label: int | None = None) Gate[source]#

Get the quasiprobability-decomposition representation.

set_controls(controls: int | list[int]) None[source]#

Set the control wires of the Operation.

update_matrix() Tensor[source]#

Update the local unitary matrix.

class GateQPD(bases: nn.ModuleList[nn.ModuleList[nn.Sequential]], coeffs: list[float], label: int | None = None, name: str | None = None, nqubit: int = 1, wires: int | list[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: Gate

A base class for quasiprobability-decomposition gates.

Parameters:
  • bases (nn.ModuleList[nn.ModuleList[nn.Sequential]]) – The probabilistic basis operations for the decomposition. A nested structure where: - bases[i] is the i-th term of the QPD (corresponding to coeffs[i]). - bases[i][j] is the nn.Sequential operations applied to the j-th qubit.

  • coeffs (list[float]) – The coefficients for quasiprobability representation.

  • label (int | None) – The label of the gate. Default: None

  • name (str | None) – The name of the quantum operation. Default: None

  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • den_mat (bool) – Whether the quantum operation acts on density matrices or state vectors. Default: False (which means state vectors)

  • tsr_mode (bool) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: False

forward(x: Tensor, idx: int | None = None) Tensor[source]#

Perform a forward pass.

Parameters:
  • x (Tensor) – The input tensor.

  • idx (int | None) – The index of the operation to be applied. Default: None

set_nqubit(nqubit: int) None[source]#

Set the number of qubits of the GateQPD.

set_wires(wires: int | list[int]) None[source]#

Set the wires of the GateQPD.

class Layer(name: str | None = None, nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: Operation

A base class for quantum layers.

Parameters:
  • name (str | None) – The name of the layer. Default: None

  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | list[list[int]] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • den_mat (bool) – Whether the quantum operation acts on density matrices or state vectors. Default: False (which means state vectors)

  • tsr_mode (bool) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: False

forward(x: Tensor | MatrixProductState | DistributedQubitState) Tensor | MatrixProductState | DistributedQubitState[source]#

Perform a forward pass.

get_unitary() Tensor[source]#

Get the global unitary matrix.

init_para(inputs: Any = None) None[source]#

Initialize the parameters.

inverse() Layer[source]#

Get the inversed layer.

pattern(nodes: list[list[int]], ancilla: list[list[int]]) Sequential[source]#

Get the MBQC pattern.

set_nqubit(nqubit: int) None[source]#

Set the number of qubits of the Layer.

set_wires(wires: int | list[int] | list[list[int]]) None[source]#

Set the wires of the Layer.

update_npara() None[source]#

Update the number of parameters.

class MeasureQPD(nqubit: int = 1, wires: int | list[int] | None = None)[source]#

Bases: Operation

A operation for denoting a QPD measurement location.

Parameters:
  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

forward(x: Any) Any[source]#

Perform a forward pass.

class Operation(name: str | None = None, nqubit: int = 1, wires: int | list[int] | None = None, den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: Module

A base class for quantum operations.

Parameters:
  • name (str | None) – The name of the quantum operation. Default: None

  • nqubit (int) – The number of qubits that the quantum operation acts on. Default: 1

  • wires (int | list[int] | None) – The indices of the qubits that the quantum operation acts on. Default: None

  • den_mat (bool) – Whether the quantum operation acts on density matrices or state vectors. Default: False (which means state vectors)

  • tsr_mode (bool) – Whether the quantum operation is in tensor mode, which means the input and output are represented by a tensor of shape \((\text{batch}, 2, ..., 2)\). Default: False

forward(x: Tensor) Tensor[source]#

Perform a forward pass.

get_unitary() Tensor[source]#

Get the global unitary matrix.

init_para() None[source]#

Initialize the parameters.

matrix_rep(x: Tensor) Tensor[source]#

Get the density matrix representation of the state.

set_nqubit(nqubit: int) None[source]#

Set the number of qubits of the Operation.

set_wires(wires: int | list[int]) None[source]#

Set the wires of the Operation.

tensor_rep(x: Tensor) Tensor[source]#

Get the tensor representation of the state.

vector_rep(x: Tensor) Tensor[source]#

Get the vector representation of the state.