layer#

Quantum layers

Classes

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

A layer of CNOT gates.

CnotRing([nqubit, minmax, step, reverse, ...])

A layer of CNOT gates in a cyclic way.

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

A base class for layers of two-qubit gates.

HLayer([nqubit, wires, den_mat, tsr_mode])

A layer of Hadamard gates.

Observable([nqubit, wires, basis, den_mat, ...])

A Layer that represents an observable which can be expressed by Pauli string.

ParametricSingleLayer([name, nqubit, wires, ...])

A base class for layers of single-qubit gates with parameters.

RxLayer([nqubit, wires, inputs, den_mat, ...])

A layer of Rx gates.

RyLayer([nqubit, wires, inputs, den_mat, ...])

A layer of Ry gates.

RzLayer([nqubit, wires, inputs, den_mat, ...])

A layer of Rz gates.

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

A base class for layers of single-qubit gates.

U3Layer([nqubit, wires, inputs, den_mat, ...])

A layer of U3 gates.

XLayer([nqubit, wires, den_mat, tsr_mode])

A layer of Pauli-X gates.

YLayer([nqubit, wires, den_mat, tsr_mode])

A layer of Pauli-Y gates.

ZLayer([nqubit, wires, den_mat, tsr_mode])

A layer of Pauli-Z gates.

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

Bases: DoubleLayer

A layer of CNOT gates.

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

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

  • name (str) – The name of the layer. Default: 'CnotLayer'

  • 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

inverse() CnotLayer[source]#

Get the inversed layer.

class CnotRing(nqubit: int = 2, minmax: list[int] | None = None, step: int = 1, reverse: bool = False, den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: CnotLayer

A layer of CNOT gates in a cyclic way.

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

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

  • step (int) – The distance between the control and target qubits of each CNOT gate. Default: 1

  • reverse (bool) – Whether the CNOT gates are applied from the maximum to the minimum index or vice versa. 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

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

Bases: Layer

A base class for layers of two-qubit gates.

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

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

  • wires (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

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

Bases: SingleLayer

A layer of Hadamard gates.

Parameters:
  • 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

class Observable(nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, basis: str = 'z', den_mat: bool = False, tsr_mode: bool = False)[source]#

Bases: SingleLayer

A Layer that represents an observable which can be expressed by Pauli string.

Parameters:
  • nqubit (int) – The number of qubits in the circuit. Default: 1

  • wires (int | list[int] | list[list[int]] | None) – The wires to measure. Default: None (which means all wires are measured)

  • basis (str) – The measurement basis for each wire. It can be 'x', 'y', or 'z'. If only one character is given, it is repeated for all wires. Default: 'z'

  • 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

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

Bases: SingleLayer

A base class for layers of single-qubit gates with parameters.

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

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

inverse() ParametricSingleLayer[source]#

Get the inversed layer.

pattern(nodes: list[list[int]], ancilla: list[list[int]], angle: list[Any], requires_grad: bool = False) Sequential[source]#

Get the MBQC pattern.

class RxLayer(nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, inputs: Any = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)[source]#

Bases: ParametricSingleLayer

A layer of Rx gates.

Parameters:
  • 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

  • inputs (Any) – The parameters of the layer. 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

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

class RyLayer(nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, inputs: Any = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)[source]#

Bases: ParametricSingleLayer

A layer of Ry gates.

Parameters:
  • 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

  • inputs (Any) – The parameters of the layer. 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

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

class RzLayer(nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, inputs: Any = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)[source]#

Bases: ParametricSingleLayer

A layer of Rz gates.

Parameters:
  • 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

  • inputs (Any) – The parameters of the layer. 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

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

class SingleLayer(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: Layer

A base class for layers of single-qubit gates.

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

get_unitary() Tensor[source]#

Get the global unitary matrix.

class U3Layer(nqubit: int = 1, wires: int | list[int] | list[list[int]] | None = None, inputs: Any = None, den_mat: bool = False, tsr_mode: bool = False, requires_grad: bool = True)[source]#

Bases: ParametricSingleLayer

A layer of U3 gates.

Parameters:
  • 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

  • inputs (Any) – The parameters of the layer. 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

  • requires_grad (bool) – Whether the parameters are nn.Parameter or buffer. Default: True (which means nn.Parameter)

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

Bases: SingleLayer

A layer of Pauli-X gates.

Parameters:
  • 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

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

Bases: SingleLayer

A layer of Pauli-Y gates.

Parameters:
  • 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

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

Bases: SingleLayer

A layer of Pauli-Z gates.

Parameters:
  • 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