qrunch.quantum.gates.gates_protocols

Base classes for quantum gates.

Classes

AnnotationGate

Annotation gates are gates we only use for annotating programs.

CompositeGate

A gate constructed from a list of more primitive gates.

Gate

Interface for quantum gates.

GateFactory

Protocol for a function that generates a composite gate from qubit indices.

UniversalGate

Universal gate type.

class AnnotationGate

Bases: Gate

Annotation gates are gates we only use for annotating programs.

We can use annotation gates as parts of composite gates or as part of a circuit to store additional information about a program. They are generally not used for computing, but can be used for validation or simulation.

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

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, ...]

Indices of the qubits that gate is working on.

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

Relocate the gate to new qubit indices.

Parameters:

new_indices (Mapping[int, int])

Return type:

Self

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 CompositeGate

Bases: Gate

A gate constructed from a list of more primitive gates.

A sequence of gates describe a unitary operation and can be considered a single gate in its own right. This way, we can construct new gates by combining existing gates.

__init__(gates: list[Gate], name: str | None = None, qubit_indices: tuple[int, ...] | None = None, symbols: list[str] | None = None, parameters: list[float | Parameter | ParameterExpression] | None = None) None

Create a new composite gate.

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

  • gates (list[Gate]) – List of gates to be combined. If None, the subclass must implement the gates property.

  • qubit_indices (tuple[int, ...] | None) – Indices of the qubits that the gate acts on. If None, the default is to list all the qubits in the gates.

  • symbols (list[str] | None) – List of symbols for the gate parameters. If None, the default is constructed from the gate’s name and qubit indices.

  • parameters (list[float | Parameter | ParameterExpression] | None) – List of parameters for the gate.

Return type:

None

static active_qubits(gates: Iterable[Gate]) set[int]

Return the active qubits in a sequence of gates.

Parameters:

gates (Iterable[Gate]) – Sequence of gates.

Return type:

set[int]

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

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.

The list of gates are the immediate sub-gates that the composite gate is constructed from, not necessarily the primitive gates. To expand the gate down to primitive gates use the primitive_gates property instead.

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

name: str
property parameters: list[float | Parameter | ParameterExpression]

List of all parameters available.

property qubit_indices: tuple[int, ...]

Indices of the qubits that gate is working on.

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

static sorted_active_qubits(gates: Iterable[Gate]) tuple[int, ...]

Return the active qubits in a sequence of gates, sorted, as a tuple.

Parameters:

gates (Iterable[Gate]) – Sequence of gates.

Return type:

tuple[int, …]

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

Assign parameters with the given float value.

Parameters:

parameter_value_pairs (dict[Parameter, float | ParameterExpression])

Return type:

Self

class Gate

Bases: ABC

Interface for quantum gates.

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

abstractmethod flatten() Iterator[Gate]

Flatten the gate into a list of primitive gates.

Return type:

Iterator[Gate]

abstract property gates: list[Gate]

List of gates that the composite gate is constructed from.

abstractmethod 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.

abstract property qubit_indices: tuple[int, ...]

Indices of the qubits that gate is working on.

abstractmethod 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:

Self

abstractmethod 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 GateFactory

Bases: Protocol

Protocol for a function that generates a composite gate from qubit indices.

__init__(*args, **kwargs)
class UniversalGate

Bases: Gate, ABC

Universal gate type.

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

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.

abstractmethod 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.

abstract property qubit_indices: tuple[int, ...]

Indices of the qubits that gate is working on.

abstractmethod 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:

Self

abstractmethod 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