Qx.Calc (Qx - Quantum Computing Simulator v0.6.0)
View SourceShared calculation engine for quantum gate operations.
This module provides the core logic for applying quantum gates to state vectors,
used by both Qx.Qubit (single-qubit calculation mode) and Qx.Register
(multi-qubit calculation mode).
Design
The module handles:
- Single-qubit gate application (direct matrix multiplication)
- Multi-qubit gate application (tensor product expansion)
- State vector transformations
This centralizes the gate application logic in one place, making it easier to optimize and maintain.
Summary
Functions
Applies a two-qubit gate (like CNOT) to a state vector.
Applies a Fredkin (controlled-SWAP) gate to the quantum state.
Applies a single-qubit gate to a state vector.
Applies a Toffoli (CCX) gate to a state vector.
Functions
@spec apply_cnot(Nx.Tensor.t(), non_neg_integer(), non_neg_integer(), pos_integer()) :: Nx.Tensor.t()
Applies a two-qubit gate (like CNOT) to a state vector.
Uses optimized direct statevector manipulation from Qx.CalcFast for improved performance (avoids building 2^n x 2^n matrix).
Parameters
state- State vector (Nx tensor)control_qubit- Index of control qubittarget_qubit- Index of target qubitnum_qubits- Total number of qubits in the system
Examples
iex> reg = Qx.Register.new(2)
iex> Qx.Calc.apply_cnot(reg.state, 0, 1, 2)
@spec apply_cswap( Nx.Tensor.t(), non_neg_integer(), non_neg_integer(), non_neg_integer(), pos_integer() ) :: Nx.Tensor.t()
Applies a Fredkin (controlled-SWAP) gate to the quantum state.
Swaps target_a and target_b when the control qubit is |1⟩.
Parameters
state- The quantum state vectorcontrol- Index of the control qubittarget_a- Index of the first target qubittarget_b- Index of the second target qubitnum_qubits- Total number of qubits in the system
@spec apply_single_qubit_gate( Nx.Tensor.t(), Nx.Tensor.t(), non_neg_integer(), pos_integer() ) :: Nx.Tensor.t()
Applies a single-qubit gate to a state vector.
Uses optimized direct statevector manipulation from Qx.CalcFast for improved performance (10-1000x faster than matrix-based approach).
Parameters
state- State vector (Nx tensor)gate_matrix- 2x2 gate matrix (from Qx.Gates)target_qubit- Index of qubit to apply gate to (0-based)num_qubits- Total number of qubits in the system
Examples
# Single qubit (num_qubits = 1, target = 0)
iex> state = Qx.Qubit.new()
iex> gate = Qx.Gates.hadamard()
iex> Qx.Calc.apply_single_qubit_gate(state, gate, 0, 1)
# Multi-qubit register (apply H to qubit 1 in 3-qubit system)
iex> reg = Qx.Register.new(3)
iex> gate = Qx.Gates.hadamard()
iex> Qx.Calc.apply_single_qubit_gate(reg.state, gate, 1, 3)
@spec apply_toffoli( Nx.Tensor.t(), non_neg_integer(), non_neg_integer(), non_neg_integer(), pos_integer() ) :: Nx.Tensor.t()
Applies a Toffoli (CCX) gate to a state vector.
Uses optimized direct statevector manipulation from Qx.CalcFast for improved performance (avoids building 2^n x 2^n matrix).
Parameters
state- State vector (Nx tensor)control1- Index of first control qubitcontrol2- Index of second control qubittarget- Index of target qubitnum_qubits- Total number of qubits in the system