gateraid package

Submodules

gateraid.controlled_gates module

Implementation of common and variable 2-qubit controlled gates.

class gateraid.controlled_gates.ControlledUnitary(single_qubit_unitary: ndarray, single_qubit_unitary_name: str, first_qubit_controlled: bool = True, anti_control: bool = False)

Bases: TwoQubitGate

A controlled unitary gate.

Parameters:
  • single_qubit_unitary (np.ndarray) – The single-qubit unitary to apply.

  • single_qubit_unitary_name (str) – The name of the single-qubit unitary.

  • first_qubit_controlled (bool, optional) – Whether the first qubit is the control qubit. Default is True.

  • anti_control (bool, optional) – Whether the control is inverted. Default is False.

Variables:
  • matrix (np.ndarray) – The unitary matrix of the gate.

  • name (str) – The name of the gate.

Examples

>>> import numpy as np
>>> from gateraid.controlled_gates import ControlledUnitary
>>> X = np.array([[0, 1], [1, 0]], dtype=complex)
>>> gate = ControlledUnitary(X, 'X')
>>> print(gate)
CX1->2 gate with matrix:
[[1+0j 0+0j 0+0j 0+0j]
 [0+0j 1+0j 0+0j 0+0j]
 [0+0j 0+0j 0+0j 1+0j]
 [0+0j 0+0j 1+0j 0+0j]]
>>> import numpy as np
>>> from gateraid.controlled_gates import ControlledUnitary
>>> X = np.array([[0, 1], [1, 0]], dtype=complex)
>>> gate = ControlledUnitary(X, 'X',
                             first_qubit_controlled=False,
                             anti_control=True)
>>> print(gate)
anti-CX2->1 gate with matrix:
[[0+0j 0+0j 1+0j 0+0j]
 [0+0j 1+0j 0+0j 0+0j]
 [1+0j 0+0j 0+0j 0+0j]
 [0+0j 0+0j 0+0j 1+0j]]
gateraid.controlled_gates.make_CX(first_qubit_controlled=True, anti_control=False)

Define the controlled-X gate.

Example

>>> import numpy as np
>>> from gateraid.controlled_gates import make_CX
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> CX = make_CX()
>>> new_state = CX(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+0j|10> +
1+0j|11>
gateraid.controlled_gates.make_CY(first_qubit_controlled=True, anti_control=False)

Define the controlled-Y gate.

Example

>>> import numpy as np
>>> from gateraid.controlled_gates import make_CY
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> CY = make_CY()
>>> new_state = CY(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+0j|10> +
0+1j|11>
gateraid.controlled_gates.make_CZ(first_qubit_controlled=True, anti_control=False)

Define the controlled-Z gate.

Example

>>> import numpy as np
>>> from gateraid.controlled_gates import make_CZ
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 0, 1]))
>>> CZ = make_CZ()
>>> new_state = CZ(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+0j|10> +
-1+0j|11>

gateraid.gate_base module

Implementation of a general 2-qubit unitary gate.

class gateraid.gate_base.TwoQubitGate(matrix: ndarray, name: str)

Bases: ABC

A 2-qubit unitary gate.

Parameters:
  • matrix (np.ndarray) – The unitary matrix of the gate.

  • name (str) – The name of the gate.

Variables:
  • matrix (np.ndarray) – The unitary matrix of the gate.

  • name (str) – The name of the gate.

__call__(state: QuantumState) QuantumState

Apply the gate to a quantum state.

__str__()

Return string representation of the gate.

__repr__()

Return canonical string representation of the gate.

Example

>>> import numpy as np
>>> from gateraid.gate_base import TwoQubitGate
>>> matrix = np.array([[1, 0, 0, 0],
                       [0, 1, 0, 0],
                       [0, 0, 0, 1],
                       [0, 0, 1, 0]], dtype=complex)
>>> name = 'CNOT'
>>> gate = TwoQubitGate(matrix, name)
>>> print(gate)
CNOT gate with matrix:
[[1+0j 0+0j 0+0j 0+0j]
 [0+0j 1+0j 0+0j 0+0j]
 [0+0j 0+0j 0+0j 1+0j]
 [0+0j 0+0j 1+0j 0+0j]]

gateraid.other_gates module

Implementation of common and variable 2-qubit gates.

class gateraid.other_gates.GeneralUnitary(pauli_dict: dict[str, float])

Bases: TwoQubitGate

Initialise a 2-qubit gate, in the form U = exp(i[∑_i c_i P_i]), where P_i are Pauli matrices and c_i the coefficients.

Parameters:

pauli_dict (dict[str, float]) – A dictionary where the keys represent 2-qubit Pauli matricies and the values are the coefficients of the Pauli matrices.

Variables:
  • matrix (np.ndarray) – The unitary matrix of the gate.

  • name (str) – The name of the gate.

Raises:
  • ValueError – If the keys of the dictionary are not 2-character strings, made up of ‘I’, ‘X’, ‘Y’ and ‘Z’.

  • ValueError – If the values of the dictionary are not real numbers.

Example

>>> import numpy as np
>>> from gateraid.other_gates import GeneralUnitary
>>> pauli_dict = {'II': 1, 'XX': 1, 'YY': 1, 'ZZ': 1}
>>> gate = GeneralUnitary(pauli_dict)
>>> print(gate)
exp(i[+1II+1XX+1YY+1ZZ]) gate with matrix:
[[-0.4161+0.9093j 0+0j 0+0j 0+0j]
 [0+0j -0.4161+0j 0+0.9093j 0+0j]
 [0+0j 0+0.9093j -0.4161+0j 0+0j]
 [0+0j 0+0j 0+0j -0.4161+0.9093j]]
class gateraid.other_gates.LocalUnitary(single_qubit_unitary: ndarray, single_qubit_unitary_name: str, site: int)

Bases: TwoQubitGate

Initialise a 2-qubit gate that acts on a single qubit.

Parameters:
  • single_qubit_unitary (np.ndarray) – The single-qubit unitary that acts on the specified site.

  • single_qubit_unitary_name (str) – The name of the single-qubit unitary.

  • site (int) – The index of the qubit that the single-qubit unitary acts on.

Variables:
  • matrix (np.ndarray) – The unitary matrix of the gate.

  • name (str) – The name of the gate.

Raises:

ValueError – If the site index is not 0 or 1.

Example

>>> import numpy as np
>>> from gateraid.other_gates import LocalUnitary
>>> X = np.array([[0, 1], [1, 0]], dtype=complex)
>>> gate = LocalUnitary(X, 'X', 0)
>>> print(gate)
X_I gate with matrix:
[[0+0j 0+0j 1+0j 0+0j]
 [0+0j 0+0j 0+0j 1+0j]
 [1+0j 0+0j 0+0j 0+0j]
 [0+0j 1+0j 0+0j 0+0j]]
gateraid.other_gates.make_H()

Define the Hadamard gate.

Example

>>> from gateraid.other_gates import make_H, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState()
>>> H = make_H()
>>> HI = LocalUnitary(H, 'H', 0)
>>> new_state = HI(state)
>>> print(new_state)
State:
0.7071+0j|00> +
0+0j|01> +
0.7071+0j|10> +
0+0j|11>
gateraid.other_gates.make_S()

Define the S gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_S, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> S = make_S()
>>> SI = LocalUnitary(S, 'S', 0)
>>> new_state = SI(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+1j|10> +
0+0j|11>
gateraid.other_gates.make_SWAP()

Define the SWAP gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_SWAP
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 1, 0, 0]))
>>> SWAP = make_SWAP()
>>> new_state = SWAP(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
1+0j|10> +
0+0j|11>
gateraid.other_gates.make_T()

Define the T gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_T, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> T = make_T()
>>> TI = LocalUnitary(T, 'T', 0)
>>> new_state = TI(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0.7071+0.7071j|10> +
0+0j|11>
gateraid.other_gates.make_X()

Define the X gate.

Example

>>> from gateraid.other_gates import make_X, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState()
>>> X = make_X()
>>> XI = LocalUnitary(X, 'X', 0)
>>> new_state = XI(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
1+0j|10> +
0+0j|11>
gateraid.other_gates.make_Y()

Define the Y gate.

Example

>>> from gateraid.other_gates import make_Y, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState()
>>> Y = make_Y()
>>> YI = LocalUnitary(Y, 'Y', 0)
>>> new_state = YI(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+1j|10> +
0+0j|11>
gateraid.other_gates.make_Z()

Define the Z gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_Z, LocalUnitary
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> Z = make_Z()
>>> ZI = LocalUnitary(Z, 'Z', 0)
>>> new_state = ZI(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
-1+0j|10> +
0+0j|11>
gateraid.other_gates.make_iSWAP()

Define the iSWAP gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_iSWAP
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 1, 0, 0]))
>>> iSWAP = make_iSWAP()
>>> new_state = iSWAP(state)
>>> print(new_state)
State:
0+0j|00> +
0+0j|01> +
0+1j|10> +
0+0j|11>
gateraid.other_gates.make_sqrt_SWAP()

Define the √SWAP gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_sqrt_SWAP
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 1, 0, 0]))
>>> sqrt_SWAP = make_sqrt_SWAP()
>>> new_state = sqrt_SWAP(state)
>>> print(new_state)
State:
0+0j|00> +
0.5+0.5j|01> +
0.5-0.5j|10> +
0+0j|11>
gateraid.other_gates.make_sqrt_iSWAP()

Define the √iSWAP gate.

Example

>>> import numpy as np
>>> from gateraid.other_gates import make_sqrt_iSWAP
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([0, 1, 0, 0]))
>>> sqrt_iSWAP = make_sqrt_iSWAP()
>>> new_state = sqrt_iSWAP(state)
>>> print(new_state)
State:
0+0j|00> +
0.7071+0j|01> +
0+0.7071j|10> +
0+0j|11>

gateraid.quantum_state module

Implementation of a 2-qubit quantum state.

class gateraid.quantum_state.QuantumState(state=None)

Bases: object

A 2-qubit quantum state.

Parameters:

state (np.ndarray, optional) – The state to initialize. Default is |00>=np.array([1, 0, 0, 0]).

Variables:

state (np.ndarray) – The state of the quantum system.

Raises:
  • ValueError – If the state is not a 4x1 column vector.

  • ValueError – If the state is not normalized.

__str__()

Return string representation of the quantum state.

__repr__()

Return canonical string representation of the quantum state.

apply_matrix(matrix: np.ndarray, _skip_validation=False) QuantumState

Apply a matrix to the state.

Examples

>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState()
>>> print(state)
State:
1+0j|00> +
0+0j|01> +
0+0j|10> +
0+0j|11>
>>> import numpy as np
>>> from gateraid.quantum_state import QuantumState
>>> state = QuantumState(np.array([1, 0, 0, 1])/np.sqrt(2))
>>> print(state)
State:
0.7071+0j|00> +
0+0j|01> +
0+0j|10> +
0.7071+0j|11>
apply_matrix(matrix: ndarray, _skip_validation=False)

Apply a matrix to the state.

Parameters:
  • matrix (np.ndarray) – The matrix to apply.

  • _skip_validation (bool, optional) – Skip the validation of the matrix, by default False.

Returns:

The updated quantum state.

Return type:

QuantumState

Example

>>> state = QuantumState(np.array([0, 0, 1, 0]))
>>> matrix = np.array([[1, 0, 0, 0],
                       [0, 1, 0, 0],
                       [0, 0, 0, 1],
                       [0, 0, 1, 0]], dtype=complex)
>>> state.apply_matrix(matrix)
>>> print(state)
State:
0+0j|00> +
0+0j|01> +
0+0j|10> +
1+0j|11>

gateraid.utilities module

Utilities for gateraid.

gateraid.utilities.check_unitary(matrix: ndarray) None

Verify that a matrix is unitary.

Parameters:

matrix (np.ndarray) – The matrix to verify.

Raises:
  • TypeError – If the matrix is not a numpy array.

  • TypeError – If the matrix is not of dtype float, complex or int.

  • ValueError – If the matrix shape is not (4, 4).

  • ValueError – If the matrix is not unitary.

Module contents