Qx.Gates (Qx - Quantum Computing Simulator v0.6.0)
View SourceQuantum 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
Returns the CNOT (controlled-X) gate matrix for n qubits.
Parameters
control_qubit- Index of the control qubittarget_qubit- Index of the target qubitnum_qubits- Total number of qubits
Examples
iex> cnot = Qx.Gates.cnot(0, 1, 2)
Creates a controlled version of a single-qubit gate for n qubits.
Parameters
gate- The single-qubit gate matrixcontrol_qubit- Index of the control qubittarget_qubit- Index of the target qubitnum_qubits- Total number of qubits
Examples
iex> cx_gate = Qx.Gates.controlled_gate(Qx.Gates.pauli_x(), 0, 1, 2)
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 qubittarget_a- Index of the first target qubittarget_b- Index of the second target qubitnum_qubits- Total number of qubits
Examples
iex> Nx.shape(Qx.Gates.cswap(0, 1, 2, 3))
{8, 8, 2}
Returns the Hadamard gate matrix.
H = 1/√2 * [[1, 1],
[1, -1]]Examples
iex> Qx.Gates.hadamard()
# Returns complex matrix tensor
Returns the identity gate matrix.
I = [[1, 0],
[0, 1]]Examples
iex> Qx.Gates.identity()
# Returns complex matrix tensor
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 qubitqubit_b- Index of the second qubitnum_qubits- Total number of qubits
Examples
iex> Nx.shape(Qx.Gates.iswap(0, 1, 2))
{4, 4}Raises
FunctionClauseError- If qubit indices are out of range or equal
Returns the Pauli-X gate matrix (bit flip).
X = [[0, 1],
[1, 0]]Examples
iex> Qx.Gates.pauli_x()
# Returns complex matrix tensor
Returns the Pauli-Y gate matrix.
Y = [[0, -i],
[i, 0]]Examples
iex> Qx.Gates.pauli_y()
# Returns complex matrix tensor
Returns the Pauli-Z gate matrix (phase flip).
Z = [[1, 0],
[0, -1]]Examples
iex> Qx.Gates.pauli_z()
# Returns complex matrix tensor
Returns a phase gate with arbitrary phase.
Phase(φ) = [[1, 0 ],
[0, e^(iφ)]]Parameters
phi- Phase angle in radians
Examples
iex> Qx.Gates.phase(:math.pi/4)
# Returns complex matrix tensor
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
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
Returns a rotation gate around the Z-axis.
RZ(θ) = [[e^(-iθ/2), 0 ],
[0, e^(iθ/2)]]Parameters
theta- Rotation angle in radians
Examples
iex> Qx.Gates.rz(math.pi/2)
# Returns complex matrix tensor
Returns the S† (S-dagger) gate matrix.
S† = [[1, 0],
[0, -i]]Examples
iex> Qx.Gates.s_dagger()
# Returns complex matrix tensor
Returns the S gate matrix (phase gate π/2).
S = [[1, 0],
[0, i]]Examples
iex> Qx.Gates.s_gate()
# Returns complex matrix tensor
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 qubitqubit_b- Index of the second qubitnum_qubits- Total number of qubits
Examples
iex> Nx.shape(Qx.Gates.swap(0, 1, 2))
{4, 4}Raises
FunctionClauseError- If qubit indices are out of range or equal
Returns the T† (T-dagger) gate matrix.
T† = [[1, 0 ],
[0, e^(-iπ/4)]]Examples
iex> Qx.Gates.t_dagger()
# Returns complex matrix tensor
Returns the T gate matrix (phase gate π/4).
T = [[1, 0 ],
[0, e^(iπ/4)]]Examples
iex> Qx.Gates.t_gate()
# Returns complex matrix tensor
Returns the Toffoli (CCX) gate matrix for n qubits.
Parameters
control1- Index of the first control qubitcontrol2- Index of the second control qubittarget- Index of the target qubitnum_qubits- Total number of qubits
Examples
iex> toffoli = Qx.Gates.toffoli(0, 1, 2, 3)
Returns the general single-qubit unitary gate (IBM/OpenQASM 3 convention).
U(θ,φ,λ) = [[cos(θ/2), -e^(iλ)·sin(θ/2) ],
[e^(iφ)·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 radiansphi- Azimuthal angle in radianslambda- Additional phase angle in radians
Examples
iex> Nx.shape(Qx.Gates.u(0, 0, 0))
{2, 2}