qrunch.quantum.gates.universal_gates

Module with implementations of universal quantum gates.

Classes

CXGate

Two qubit controlled X-gate (CX or CNOT).

PGate

Phase gate.

RXGate

Single qubit X-rotation.

RYGate

Single qubit Y-rotation.

RZGate

Single qubit Z-rotation.

class CXGate

Bases: UniversalGate

Two qubit controlled X-gate (CX or CNOT). The CX gate acts on two qubits as.

\[\begin{split}\text{CX} |00\rangle = |00\rangle, \\ \text{CX} |01\rangle = |01\rangle, \\ \text{CX} |10\rangle = |11\rangle, \\ \text{CX} |11\rangle = |01\rangle.\end{split}\]

Circuit form:

────●────
    │
  ┌─┴─┐
──┤ X ├──
  └───┘

Matrix representation in the basis \((|00\rangle, |01\rangle, |10\rangle, |11\rangle)\):

\[\begin{split}\text{CX} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}\end{split}\]
__init__(control_qubit_index: int, target_qubit_index: int) None

Initialize a CX-gate for a control and target qubit.

Indices control_qubit_index and target_qubit_index, respectively.

Parameters:
  • control_qubit_index (int) – Index of the control qubit.

  • target_qubit_index (int) – Index of the qubit for the X-gate to be conditionally applied on.

Return type:

None

commutes_with_pauli_string(pauli_string: PauliString) bool

Check if gate commutes with the given pauli string. Default is False.

The default implementation always returns False. So False is considered a safe answer. An only True should be trusted.

Parameters:

pauli_string (PauliString) – Pauli string to check commutation for.

Return type:

bool

classmethod decode(data: dict[str, Any]) CXGate

Decode a dictionary.

Parameters:

data (dict[str, Any]) – The dictionary representation.

Return type:

CXGate

encode() dict[str, Any]

Encode the instance into a dictionary.

Return type:

dict[str, Any]

flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

property gates: list[Gate]

List of gates that the composite gate is constructed from.

invert() Self

Get the inverse of the gate.

Return type:

Self

is_fully_specified() bool

Check if all parameters have been specified.

Return type:

bool

property parameters: list[float | Parameter | ParameterExpression]

List of all parameters available.

property qubit_indices: tuple[int, int]

Tuple containing the qubit indices for the gate as (control_qubit_index, target_qubit_index).

relocate(new_indices: Mapping[int, int]) Self

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int]) – Mapping between current qubit indices and new qubit indices.

Returns:

New gate with the qubit indices updated.

Return type:

Gate

symbols(*, max_number_of_decimals: int | None = None) list[str]

Symbols for represent the gate in a circuit.

Parameters:

max_number_of_decimals (int | None) – Maximum number of decimals to use for floats in the symbols. Defaults to None.

Return type:

list[str]

with_specified_parameters(parameter_value_pairs: dict[Parameter, float | ParameterExpression]) Self

Create a new gate with the specified parameters.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression]) – Mapping between parameters and their values.

Returns:

New gate with the parameters specified.

Return type:

Self

class PGate

Bases: UniversalGate, GateEncodingMixin

Phase gate.

\[\begin{split}P(\phi) = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{bmatrix}\end{split}\]

Circuit form:

  ┌─────┐
──┤P(φ) ├──
  └─────┘
__init__(parameter: Parameter | ParameterExpression | float, qubit_index: int) None

Initialize a P-gate for a qubit with the given index qubit_index.

Parameters:
  • parameter (Parameter | ParameterExpression | float) – Parameter of the gate.

  • qubit_index (int) – Index of the qubit for the P-gate to act on.

Return type:

None

commutes_with_pauli_string(pauli_string: PauliString) bool

Check if gate commutes with the given pauli string. Default is False.

The default implementation always returns False. So False is considered a safe answer. An only True should be trusted.

Parameters:

pauli_string (PauliString) – Pauli string to check commutation for.

Return type:

bool

classmethod decode(data: dict[str, Any]) T

Decode a dictionary.

Parameters:

data (dict[str, Any]) – The dictionary representation.

Return type:

T

encode() dict[str, Any]

Encode the instance into a dictionary.

Return type:

dict[str, Any]

flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

property gates: list[Gate]

List of gates that the composite gate is constructed from.

invert() Self

Get the inverse of the gate.

Return type:

Self

is_fully_specified() bool

Check if all parameters have been specified.

Return type:

bool

property parameters: list[float | Parameter | ParameterExpression]

List of parameters for the gate.

property qubit_indices: tuple[int]

Tuple containing the qubit indices for the gate as (qubit_index).

relocate(new_indices: Mapping[int, int]) Self

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int]) – Mapping between current qubit indices and new qubit indices.

Returns:

New gate with the qubit indices updated.

Return type:

Gate

symbols(*, max_number_of_decimals: int | None = None) list[str]

Symbols for represent the gate in a circuit.

Parameters:

max_number_of_decimals (int | None) – Maximum number of decimals to use for floats in the symbols. Defaults to None.

Return type:

list[str]

with_specified_parameters(parameter_value_pairs: dict[Parameter, float | ParameterExpression]) PGate

Update the gate parameters with the values contained in parameter_value_pairs.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression]) – Parameters and values to be specified in the gate.

Returns:

New gate equivalent to the original gate but with the parameters specified.

Return type:

PGate

class RXGate

Bases: UniversalGate, GateEncodingMixin

Single qubit X-rotation. The operator form of the Rx-gate is.

\[R_x(\theta) = \cos \theta/2 I -i \sin \theta/2 X ,\]

where X is the pauli X operator from XGate.

Circuit form:

  ┌─────┐
──┤Rx(θ)├──
  └─────┘

Matrix representation in the basis \((|0\rangle, |1\rangle)\):

\[\begin{split}R_x(\theta) = \begin{bmatrix} \cos \theta/2 & -i\sin \theta/2 \\ -i\sin \theta/2 & \cos \theta/2 \end{bmatrix}\end{split}\]
__init__(parameter: Parameter | ParameterExpression | float, qubit_index: int) None

Initialize an RX-gate for a qubit with the given index qubit_index.

Parameters:
  • parameter (Parameter | ParameterExpression | float) – Parameter of the gate.

  • qubit_index (int) – Index of the qubit for the RX-gate to act on.

Return type:

None

commutes_with_pauli_string(pauli_string: PauliString) bool

Check if gate commutes with the given pauli string. Default is False.

The default implementation always returns False. So False is considered a safe answer. An only True should be trusted.

Parameters:

pauli_string (PauliString) – Pauli string to check commutation for.

Return type:

bool

classmethod decode(data: dict[str, Any]) T

Decode a dictionary.

Parameters:

data (dict[str, Any]) – The dictionary representation.

Return type:

T

encode() dict[str, Any]

Encode the instance into a dictionary.

Return type:

dict[str, Any]

flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

property gates: list[Gate]

List of gates that the composite gate is constructed from.

invert() Self

Get the inverse of the gate.

Return type:

Self

is_fully_specified() bool

Check if all parameters have been specified.

Return type:

bool

property parameters: list[float | Parameter | ParameterExpression]

List of parameters for the gate.

property qubit_indices: tuple[int]

Tuple containing the qubit indices for the gate as (qubit_index).

relocate(new_indices: Mapping[int, int]) Self

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int]) – Mapping between current qubit indices and new qubit indices.

Returns:

New gate with the qubit indices updated.

Return type:

Gate

symbols(*, max_number_of_decimals: int | None = None) list[str]

Symbols for represent the gate in a circuit.

Parameters:

max_number_of_decimals (int | None) – Maximum number of decimals to use for floats in the symbols. Defaults to None.

Return type:

list[str]

with_specified_parameters(parameter_value_pairs: dict[Parameter, float | ParameterExpression]) RXGate

Update the gate parameters with the values contained in parameter_value_pairs.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression]) – Parameters and values to be specified in the gate.

Returns:

New gate equivalent to the original gate but with the parameters specified.

Return type:

RXGate

class RYGate

Bases: UniversalGate, GateEncodingMixin

Single qubit Y-rotation. The operator form of the Ry-gate is.

\[R_Y(\theta) = \cos \theta/2 I -i \sin \theta/2 Y ,\]

where Y is the pauli Y operator which acts on qubits as

\[\begin{split}Y|0\rangle &= i|1\rangle, \\ Y|1\rangle &= -i|0\rangle.\end{split}\]

Circuit form:

  ┌─────┐
──┤Ry(θ)├──
  └─────┘

Matrix representation in the basis \((|0\rangle, |1\rangle)\):

\[\begin{split}R_y(\theta) = \begin{bmatrix} \cos \theta/2 & -\sin \theta/2 \\ \sin \theta/2 & \cos \theta/2 \end{bmatrix}\end{split}\]
__init__(parameter: Parameter | ParameterExpression | float, qubit_index: int) None

Initialize an RY-gate for a qubit with the given index qubit_index.

Parameters:
  • parameter (Parameter | ParameterExpression | float) – Parameter of the gate.

  • qubit_index (int) – Index of the qubit for the RY-gate to act on.

Return type:

None

commutes_with_pauli_string(pauli_string: PauliString) bool

Check if gate commutes with the given pauli string. Default is False.

The default implementation always returns False. So False is considered a safe answer. An only True should be trusted.

Parameters:

pauli_string (PauliString) – Pauli string to check commutation for.

Return type:

bool

classmethod decode(data: dict[str, Any]) T

Decode a dictionary.

Parameters:

data (dict[str, Any]) – The dictionary representation.

Return type:

T

encode() dict[str, Any]

Encode the instance into a dictionary.

Return type:

dict[str, Any]

flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

property gates: list[Gate]

List of gates that the composite gate is constructed from.

invert() Self

Get the inverse of the gate.

Return type:

Self

is_fully_specified() bool

Check if all parameters have been specified.

Return type:

bool

property parameters: list[float | Parameter | ParameterExpression]

List of parameters for the gate.

property qubit_indices: tuple[int]

Tuple containing the qubit indices for the gate as (qubit_index).

relocate(new_indices: Mapping[int, int]) Self

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int]) – Mapping between current qubit indices and new qubit indices.

Returns:

New gate with the qubit indices updated.

Return type:

Gate

symbols(*, max_number_of_decimals: int | None = None) list[str]

Symbols for represent the gate in a circuit.

Parameters:

max_number_of_decimals (int | None) – Maximum number of decimals to use for floats in the symbols. Defaults to None.

Return type:

list[str]

with_specified_parameters(parameter_value_pairs: dict[Parameter, float | ParameterExpression]) RYGate

Update the gate parameters with the values contained in parameter_value_pairs.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression]) – Parameters and values to be specified in the gate.

Returns:

New gate equivalent to the original gate but with the parameters specified.

Return type:

RYGate

class RZGate

Bases: UniversalGate, GateEncodingMixin

Single qubit Z-rotation. The operator form of the Rz-gate is.

\[R_Z(\theta) = \cos \theta/2 I -i \sin \theta/2 Z ,\]

where Z is the pauli Z operator which acts on qubits as

\[\begin{split}Z|0\rangle &= |0\rangle, \\ Z|1\rangle &= -|1\rangle.\end{split}\]

Circuit form:

  ┌─────┐
──┤Rz(θ)├──
  └─────┘

Matrix representation in the basis \((|0\rangle, |1\rangle)\):

\[\begin{split}R_z(\theta) = \begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
__init__(parameter: Parameter | ParameterExpression | float, qubit_index: int) None

Initialize a RZ-gate for a qubit with the given index qubit_index.

Parameters:
  • parameter (Parameter | ParameterExpression | float) – Parameter of the gate.

  • qubit_index (int) – Index of the qubit for the RZ-gate to act on.

Return type:

None

commutes_with_pauli_string(pauli_string: PauliString) bool

Check if gate commutes with the given pauli string. Default is False.

The default implementation always returns False. So False is considered a safe answer. An only True should be trusted.

Parameters:

pauli_string (PauliString) – Pauli string to check commutation for.

Return type:

bool

classmethod decode(data: dict[str, Any]) T

Decode a dictionary.

Parameters:

data (dict[str, Any]) – The dictionary representation.

Return type:

T

encode() dict[str, Any]

Encode the instance into a dictionary.

Return type:

dict[str, Any]

flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

property gates: list[Gate]

List of gates that the composite gate is constructed from.

invert() Self

Get the inverse of the gate.

Return type:

Self

is_fully_specified() bool

Check if all parameters have been specified.

Return type:

bool

property parameters: list[float | Parameter | ParameterExpression]

List of parameters for the gate.

property qubit_indices: tuple[int]

Tuple containing the qubit indices for the gate as (qubit_index).

relocate(new_indices: Mapping[int, int]) Self

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int]) – Mapping between current qubit indices and new qubit indices.

Returns:

New gate with the qubit indices updated.

Return type:

Gate

symbols(*, max_number_of_decimals: int | None = None) list[str]

Symbols for represent the gate in a circuit.

Parameters:

max_number_of_decimals (int | None) – Maximum number of decimals to use for floats in the symbols. Defaults to None.

Return type:

list[str]

with_specified_parameters(parameter_value_pairs: dict[Parameter, float | ParameterExpression]) RZGate

Update the gate parameters with the values contained in parameter_value_pairs.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression]) – Parameters and values to be specified in the gate.

Returns:

New gate equivalent to the original gate but with the parameters specified.

Return type:

RZGate