Qx.Gates (Qx - Quantum Computing Simulator v0.6.0)

View Source

Quantum gate matrix definitions with proper complex number support.

This module provides the fundamental gate matrices used in quantum computing, properly implemented with complex numbers where required.

Summary

Functions

Returns the CNOT (controlled-X) gate matrix for n qubits.

Creates a controlled version of a single-qubit gate for n qubits.

Returns the Fredkin (CSWAP) gate matrix for n qubits.

Returns the Hadamard gate matrix.

Returns the identity gate matrix.

Returns the iSWAP gate matrix for n qubits.

Returns the Pauli-X gate matrix (bit flip).

Returns the Pauli-Y gate matrix.

Returns the Pauli-Z gate matrix (phase flip).

Returns a phase gate with arbitrary phase.

Returns a rotation gate around the X-axis.

Returns a rotation gate around the Y-axis.

Returns a rotation gate around the Z-axis.

Returns the S† (S-dagger) gate matrix.

Returns the S gate matrix (phase gate π/2).

Returns the SWAP gate matrix for n qubits.

Returns the T† (T-dagger) gate matrix.

Returns the T gate matrix (phase gate π/4).

Returns the Toffoli (CCX) gate matrix for n qubits.

Returns the general single-qubit unitary gate (IBM/OpenQASM 3 convention).

Functions

cnot(control_qubit, target_qubit, num_qubits)

Returns the CNOT (controlled-X) gate matrix for n qubits.

Parameters

  • control_qubit - Index of the control qubit
  • target_qubit - Index of the target qubit
  • num_qubits - Total number of qubits

Examples

iex> cnot = Qx.Gates.cnot(0, 1, 2)

controlled_gate(gate, control_qubit, target_qubit, num_qubits)

Creates a controlled version of a single-qubit gate for n qubits.

Parameters

  • gate - The single-qubit gate matrix
  • control_qubit - Index of the control qubit
  • target_qubit - Index of the target qubit
  • num_qubits - Total number of qubits

Examples

iex> cx_gate = Qx.Gates.controlled_gate(Qx.Gates.pauli_x(), 0, 1, 2)

cswap(control, target_a, target_b, num_qubits)

Returns the Fredkin (CSWAP) gate matrix for n qubits.

Swaps target_a and target_b when the control qubit is |1⟩.

Parameters

  • control - Index of the control qubit
  • target_a - Index of the first target qubit
  • target_b - Index of the second target qubit
  • num_qubits - Total number of qubits

Examples

iex> Nx.shape(Qx.Gates.cswap(0, 1, 2, 3))
{8, 8, 2}

hadamard()

Returns the Hadamard gate matrix.

H = 1/√2 * [[1, 1],

        [1, -1]]

Examples

iex> Qx.Gates.hadamard()
# Returns complex matrix tensor

identity()

Returns the identity gate matrix.

I = [[1, 0],

 [0, 1]]

Examples

iex> Qx.Gates.identity()
# Returns complex matrix tensor

iswap(qubit_a, qubit_b, num_qubits)

Returns the iSWAP gate matrix for n qubits.

Exchanges qubit states while applying an i phase to the swapped components. Matrix: [[1,0,0,0],[0,0,i,0],[0,i,0,0],[0,0,0,1]] in the two-qubit subspace.

Parameters

  • qubit_a - Index of the first qubit
  • qubit_b - Index of the second qubit
  • num_qubits - Total number of qubits

Examples

iex> Nx.shape(Qx.Gates.iswap(0, 1, 2))
{4, 4}

Raises

pauli_x()

Returns the Pauli-X gate matrix (bit flip).

X = [[0, 1],

 [1, 0]]

Examples

iex> Qx.Gates.pauli_x()
# Returns complex matrix tensor

pauli_y()

Returns the Pauli-Y gate matrix.

Y = [[0, -i],

 [i,  0]]

Examples

iex> Qx.Gates.pauli_y()
# Returns complex matrix tensor

pauli_z()

Returns the Pauli-Z gate matrix (phase flip).

Z = [[1, 0],

 [0, -1]]

Examples

iex> Qx.Gates.pauli_z()
# Returns complex matrix tensor

phase(phi)

Returns a phase gate with arbitrary phase.

Phase(φ) = [[1, 0 ],

        [0, e^()]]

Parameters

  • phi - Phase angle in radians

Examples

iex> Qx.Gates.phase(:math.pi/4)
# Returns complex matrix tensor

rx(theta)

Returns a rotation gate around the X-axis.

RX(θ) = [[cos(θ/2), -i*sin(θ/2)],

     [-i*sin(θ/2), cos(θ/2)]]

Parameters

  • theta - Rotation angle in radians

Examples

iex> Qx.Gates.rx(math.pi/2)
# Returns complex matrix tensor

ry(theta)

Returns a rotation gate around the Y-axis.

RY(θ) = [[cos(θ/2), -sin(θ/2)],

     [sin(θ/2),  cos(θ/2)]]

Parameters

  • theta - Rotation angle in radians

Examples

iex> Qx.Gates.ry(math.pi/2)
# Returns complex matrix tensor

rz(theta)

Returns a rotation gate around the Z-axis.

RZ(θ) = [[e^(-iθ/2), 0 ],

     [0,          e^(/2)]]

Parameters

  • theta - Rotation angle in radians

Examples

iex> Qx.Gates.rz(math.pi/2)
# Returns complex matrix tensor

s_dagger()

Returns the S† (S-dagger) gate matrix.

S† = [[1, 0],

  [0, -i]]

Examples

iex> Qx.Gates.s_dagger()
# Returns complex matrix tensor

s_gate()

Returns the S gate matrix (phase gate π/2).

S = [[1, 0],

 [0, i]]

Examples

iex> Qx.Gates.s_gate()
# Returns complex matrix tensor

swap(qubit_a, qubit_b, num_qubits)

Returns the SWAP gate matrix for n qubits.

Exchanges the quantum states of qubit_a and qubit_b.

Parameters

  • qubit_a - Index of the first qubit
  • qubit_b - Index of the second qubit
  • num_qubits - Total number of qubits

Examples

iex> Nx.shape(Qx.Gates.swap(0, 1, 2))
{4, 4}

Raises

t_dagger()

Returns the T† (T-dagger) gate matrix.

T† = [[1, 0 ],

  [0, e^(-/4)]]

Examples

iex> Qx.Gates.t_dagger()
# Returns complex matrix tensor

t_gate()

Returns the T gate matrix (phase gate π/4).

T = [[1, 0 ],

 [0, e^(/4)]]

Examples

iex> Qx.Gates.t_gate()
# Returns complex matrix tensor

toffoli(control1, control2, target, num_qubits)

Returns the Toffoli (CCX) gate matrix for n qubits.

Parameters

  • control1 - Index of the first control qubit
  • control2 - Index of the second control qubit
  • target - Index of the target qubit
  • num_qubits - Total number of qubits

Examples

iex> toffoli = Qx.Gates.toffoli(0, 1, 2, 3)

u(theta, phi, lambda)

Returns the general single-qubit unitary gate (IBM/OpenQASM 3 convention).

U(θ,φ,λ) = [[cos(θ/2), -e^(iλ)·sin(θ/2) ],

         [e^()·sin(θ/2),  e^(i(φ+λ))·cos(θ/2) ]]

Special cases:

  • U(π, 0, π) = X gate
  • U(π/2, 0, π) = H gate
  • U(π, π/2, π/2) = Y gate (up to global phase)

Parameters

  • theta - Polar angle in radians
  • phi - Azimuthal angle in radians
  • lambda - Additional phase angle in radians

Examples

iex> Nx.shape(Qx.Gates.u(0, 0, 0))
{2, 2}