Qx.Register (Qx - Quantum Computing Simulator v0.5.0)
View SourceMulti-qubit quantum register for calculation mode.
This module provides functionality for creating and manipulating quantum registers
containing multiple qubits. Gates are applied immediately in real-time, similar to
Qx.Qubit but for multi-qubit systems.
Calculation Mode - Multi-Qubit
Calculation mode with registers allows you to work with multiple qubits directly, applying gates in real-time and seeing results immediately. This is perfect for:
- Creating entangled states (Bell states, GHZ states)
- Multi-qubit gate exploration
- Learning quantum algorithms
- Interactive debugging
Example Workflows
# Create a 2-qubit register and make a Bell state
reg = Qx.Register.new(2)
|> Qx.Register.h(0)
|> Qx.Register.cx(0, 1)
|> Qx.Register.show_state()
# Create register from existing qubits
q1 = Qx.Qubit.new(0.6, 0.8)
q2 = Qx.Qubit.new()
reg = Qx.Register.new([q1, q2])
|> Qx.Register.h(0)
# Inspect state at any point
reg = Qx.Register.new(3)
|> Qx.Register.h(0)
|> Qx.Register.h(1)
|> Qx.Register.h(2)
probs = Qx.Register.get_probabilities(reg)
# All 8 basis states have equal probabilitySee Also
Qx.Qubit- Single qubit calculation modeQx.QuantumCircuit- Circuit mode for building quantum circuits
Summary
Functions
Applies a Toffoli (CCX/CCNOT) gate.
Applies a CNOT (controlled-X) gate.
Applies a controlled-Z gate.
Creates a register from a list of computational basis states.
Creates a register where all qubits are in superposition (|+⟩ state).
Returns the measurement probabilities for all basis states.
Applies a Hadamard gate to a specific qubit in the register.
Creates a new quantum register.
Applies a phase gate to a specific qubit.
Applies a rotation around the X-axis to a specific qubit.
Applies a rotation around the Y-axis to a specific qubit.
Applies a rotation around the Z-axis to a specific qubit.
Applies an S gate to a specific qubit in the register.
Returns a human-readable representation of the register state.
Returns the state vector of the register.
Applies a T gate to a specific qubit in the register.
Checks if a register is valid (properly normalized).
Applies a Pauli-X gate to a specific qubit in the register.
Applies a Pauli-Y gate to a specific qubit in the register.
Applies a Pauli-Z gate to a specific qubit in the register.
Types
@type t() :: %Qx.Register{num_qubits: pos_integer(), state: Nx.Tensor.t()}
Functions
Applies a Toffoli (CCX/CCNOT) gate.
Parameters
register- The quantum registercontrol1- Index of first control qubitcontrol2- Index of second control qubittarget- Index of target qubit
Examples
iex> reg = Qx.Register.new(3) |> Qx.Register.ccx(0, 1, 2)
iex> Qx.Register.valid?(reg)
true
Applies a CNOT (controlled-X) gate.
Parameters
register- The quantum registercontrol_qubit- Index of the control qubittarget_qubit- Index of the target qubit
Examples
# Create a Bell state
iex> reg = Qx.Register.new(2)
...> |> Qx.Register.h(0)
...> |> Qx.Register.cx(0, 1)
iex> Qx.Register.valid?(reg)
true
Applies a controlled-Z gate.
Parameters
register- The quantum registercontrol_qubit- Index of the control qubittarget_qubit- Index of the target qubit
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.cz(0, 1)
iex> Qx.Register.valid?(reg)
true
@spec from_basis_states([0 | 1]) :: t()
Creates a register from a list of computational basis states.
Parameters
basis_states- List of 0s and 1s representing the basis state for each qubit
Examples
# Create |010⟩ state
iex> reg = Qx.Register.from_basis_states([0, 1, 0])
iex> reg.num_qubits
3
# Create |11⟩ state (Bell state preparation starting point)
iex> reg = Qx.Register.from_basis_states([1, 1])
iex> probs = Qx.Register.get_probabilities(reg) |> Nx.to_flat_list()
iex> Enum.at(probs, 3)
1.0
@spec from_superposition(pos_integer()) :: t()
Creates a register where all qubits are in superposition (|+⟩ state).
Parameters
num_qubits- Number of qubits to create
Examples
# Create 2-qubit register in equal superposition of all 4 basis states
iex> reg = Qx.Register.from_superposition(2)
iex> probs = Qx.Register.get_probabilities(reg) |> Nx.to_flat_list()
iex> Enum.all?(probs, fn p -> abs(p - 0.25) < 0.01 end)
true
# 3-qubit superposition (8 equal states)
iex> reg = Qx.Register.from_superposition(3)
iex> probs = Qx.Register.get_probabilities(reg) |> Nx.to_flat_list()
iex> Enum.all?(probs, fn p -> abs(p - 0.125) < 0.01 end)
true
Returns the measurement probabilities for all basis states.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.h(0) |> Qx.Register.h(1)
iex> probs = Qx.Register.get_probabilities(reg)
iex> Nx.shape(probs)
{4}
Applies a Hadamard gate to a specific qubit in the register.
Parameters
register- The quantum registerqubit_index- Index of the qubit to apply the gate to (0-based)
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.h(0)
iex> Qx.Register.valid?(reg)
true
Creates a new quantum register.
Parameters
When called with an integer:
num_qubits- Number of qubits to create (all initialized to |0⟩)
When called with a list:
qubits- List of qubit state tensors to combine via tensor product
Examples
# Create a 2-qubit register (both in |0⟩)
iex> reg = Qx.Register.new(2)
iex> reg.num_qubits
2
# Create from existing qubits
iex> q1 = Qx.Qubit.new(0.6, 0.8)
iex> q2 = Qx.Qubit.new()
iex> reg = Qx.Register.new([q1, q2])
iex> reg.num_qubits
2
Applies a phase gate to a specific qubit.
Parameters
register- The quantum registerqubit_index- Index of the qubitphi- Phase angle in radians
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.phase(0, :math.pi() / 4)
iex> Qx.Register.valid?(reg)
true
Applies a rotation around the X-axis to a specific qubit.
Parameters
register- The quantum registerqubit_index- Index of the qubittheta- Rotation angle in radians
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.rx(0, :math.pi() / 2)
iex> Qx.Register.valid?(reg)
true
Applies a rotation around the Y-axis to a specific qubit.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.ry(0, :math.pi() / 2)
iex> Qx.Register.valid?(reg)
true
Applies a rotation around the Z-axis to a specific qubit.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.rz(0, :math.pi() / 4)
iex> Qx.Register.valid?(reg)
true
Applies an S gate to a specific qubit in the register.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.s(0)
iex> Qx.Register.valid?(reg)
true
Returns a human-readable representation of the register state.
Similar to Qx.Qubit.show_state/1 but for multi-qubit systems.
Examples
# Bell state
iex> reg = Qx.Register.new(2) |> Qx.Register.h(0) |> Qx.Register.cx(0, 1)
iex> info = Qx.Register.show_state(reg)
iex> is_map(info)
true
Returns the state vector of the register.
Examples
iex> reg = Qx.Register.new(2)
iex> state = Qx.Register.state_vector(reg)
iex> Nx.shape(state)
{4}
Applies a T gate to a specific qubit in the register.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.t(0)
iex> Qx.Register.valid?(reg)
true
Checks if a register is valid (properly normalized).
Examples
iex> reg = Qx.Register.new(2)
iex> Qx.Register.valid?(reg)
true
Applies a Pauli-X gate to a specific qubit in the register.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.x(0)
iex> Qx.Register.valid?(reg)
true
Applies a Pauli-Y gate to a specific qubit in the register.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.y(0)
iex> Qx.Register.valid?(reg)
true
Applies a Pauli-Z gate to a specific qubit in the register.
Examples
iex> reg = Qx.Register.new(2) |> Qx.Register.z(0)
iex> Qx.Register.valid?(reg)
true