Qx.Register (Qx - Quantum Computing Simulator v0.5.0)

View Source

Multi-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 probability

See Also

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

t()

@type t() :: %Qx.Register{num_qubits: pos_integer(), state: Nx.Tensor.t()}

Functions

ccx(register, control1, control2, target)

Applies a Toffoli (CCX/CCNOT) gate.

Parameters

  • register - The quantum register
  • control1 - Index of first control qubit
  • control2 - Index of second control qubit
  • target - Index of target qubit

Examples

iex> reg = Qx.Register.new(3) |> Qx.Register.ccx(0, 1, 2)
iex> Qx.Register.valid?(reg)
true

cx(register, control_qubit, target_qubit)

Applies a CNOT (controlled-X) gate.

Parameters

  • register - The quantum register
  • control_qubit - Index of the control qubit
  • target_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

cz(register, control_qubit, target_qubit)

Applies a controlled-Z gate.

Parameters

  • register - The quantum register
  • control_qubit - Index of the control qubit
  • target_qubit - Index of the target qubit

Examples

iex> reg = Qx.Register.new(2) |> Qx.Register.cz(0, 1)
iex> Qx.Register.valid?(reg)
true

from_basis_states(states)

@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

from_superposition(num_qubits)

@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

get_probabilities(register)

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}

h(register, qubit_index)

Applies a Hadamard gate to a specific qubit in the register.

Parameters

  • register - The quantum register
  • qubit_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

new(num_qubits)

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

phase(register, qubit_index, phi)

Applies a phase gate to a specific qubit.

Parameters

  • register - The quantum register
  • qubit_index - Index of the qubit
  • phi - Phase angle in radians

Examples

iex> reg = Qx.Register.new(2) |> Qx.Register.phase(0, :math.pi() / 4)
iex> Qx.Register.valid?(reg)
true

rx(register, qubit_index, theta)

Applies a rotation around the X-axis to a specific qubit.

Parameters

  • register - The quantum register
  • qubit_index - Index of the qubit
  • theta - Rotation angle in radians

Examples

iex> reg = Qx.Register.new(2) |> Qx.Register.rx(0, :math.pi() / 2)
iex> Qx.Register.valid?(reg)
true

ry(register, qubit_index, theta)

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

rz(register, qubit_index, theta)

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

s(register, qubit_index)

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

show_state(register)

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

state_vector(register)

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}

t(register, qubit_index)

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

valid?(register)

@spec valid?(t()) :: boolean()

Checks if a register is valid (properly normalized).

Examples

iex> reg = Qx.Register.new(2)
iex> Qx.Register.valid?(reg)
true

x(register, qubit_index)

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

y(register, qubit_index)

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

z(register, qubit_index)

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