Qx.QuantumCircuit (Qx - Quantum Computing Simulator v0.11.0)
View SourceTier 1: a core Qx type. Circuits are created and threaded by the Qx.*
facade (Qx.create_circuit/2, the gate builders, Qx.run/2); direct use
of this module is rarely needed.
Functions for creating and managing quantum circuits.
This module provides the core structure for quantum circuits, maintaining circuit state and instruction lists that can be passed to the simulator for execution.
Summary
Functions
Gets the depth (number of instruction layers) of the circuit.
Gets the list of instructions in the circuit.
Gets the list of measurements in the circuit.
Gets the initial quantum state of the circuit.
Returns the circuit's initial state vector — the |0…0⟩ recipe start,
not the result of running the circuit.
Checks if a qubit has been measured.
Creates a new quantum circuit with only qubits (no classical bits).
Creates a new quantum circuit with specified number of qubits and classical bits.
Resets the circuit to its initial state, clearing all instructions and measurements.
Sets the quantum state of the circuit.
Types
@type t() :: %Qx.QuantumCircuit{ instructions: [instruction()], measured_qubits: MapSet.t(), measurements: [measurement()], num_classical_bits: integer(), num_qubits: integer(), state: Nx.Tensor.t() }
Functions
@spec depth(t()) :: non_neg_integer()
Gets the depth (number of instruction layers) of the circuit.
Examples
iex> qc = Qx.create_circuit(2) |> Qx.h(0) |> Qx.x(1)
iex> Qx.QuantumCircuit.depth(qc)
2
@spec get_instructions(t()) :: [instruction()]
Gets the list of instructions in the circuit.
Examples
iex> qc = Qx.create_circuit(2) |> Qx.h(0)
iex> [{gate_name, qubits, _params}] = Qx.QuantumCircuit.get_instructions(qc)
iex> {gate_name, qubits}
{:h, [0]}
@spec get_measurements(t()) :: [measurement()]
Gets the list of measurements in the circuit.
Examples
iex> qc = Qx.create_circuit(2, 2) |> Qx.measure(0, 0)
iex> [{qubit, classical_bit}] = Qx.QuantumCircuit.get_measurements(qc)
iex> {qubit, classical_bit}
{0, 0}
@spec get_state(t()) :: Nx.Tensor.t()
Gets the initial quantum state of the circuit.
Examples
iex> qc = Qx.QuantumCircuit.new(1, 0)
iex> state = Qx.QuantumCircuit.get_state(qc)
iex> Nx.shape(state)
{2}
@spec initial_state(t()) :: Nx.Tensor.t()
Returns the circuit's initial state vector — the |0…0⟩ recipe start,
not the result of running the circuit.
A Qx.QuantumCircuit is a recipe: gates are recorded, then applied when you
run it. This returns the stored starting state (circuit.state). To get the
state after the gates, run the circuit: Qx.get_state/1 (1-qubit) or
Qx.run/2 + Qx.SimulationResult. See also reset/1 and depth/1.
Returns
The initial state vector as an Nx.Tensor of shape {2^n}.
Examples
iex> qc = Qx.QuantumCircuit.new(1, 0)
iex> state = Qx.QuantumCircuit.initial_state(qc)
iex> Nx.shape(state)
{2}
@spec measured?(t(), non_neg_integer()) :: boolean()
Checks if a qubit has been measured.
Examples
iex> qc = Qx.create_circuit(2, 2) |> Qx.measure(0, 0)
iex> Qx.QuantumCircuit.measured?(qc, 0)
true
iex> Qx.QuantumCircuit.measured?(qc, 1)
false
@spec new(pos_integer()) :: t()
Creates a new quantum circuit with only qubits (no classical bits).
Parameters
num_qubits- Number of qubits in the circuit
Examples
iex> qc = Qx.QuantumCircuit.new(3)
iex> qc.num_qubits
3
iex> qc.num_classical_bits
0
@spec new(pos_integer(), non_neg_integer()) :: t()
Creates a new quantum circuit with specified number of qubits and classical bits.
All qubits are initialized in the |0⟩ state, and all classical bits are initialized to 0.
Parameters
num_qubits- Number of qubits in the circuitnum_classical_bits- Number of classical bits for measurement storage
Examples
iex> qc = Qx.QuantumCircuit.new(2, 2)
iex> qc.num_qubits
2
iex> qc.num_classical_bits
2
Resets the circuit to its initial state, clearing all instructions and measurements.
Note: this clears the entire circuit (instructions + measurements + state),
not a single qubit. A future mid-circuit qubit reset (ROADMAP v0.9) will
add a distinct operation; this function may be renamed to clear/1 at
that point to disambiguate.
Examples
iex> qc = Qx.create_circuit(2, 2) |> Qx.h(0) |> Qx.measure(0, 0)
iex> qc_reset = Qx.QuantumCircuit.reset(qc)
iex> length(qc_reset.instructions)
0
iex> length(qc_reset.measurements)
0
@spec set_state(t(), Nx.Tensor.t()) :: t()
Sets the quantum state of the circuit.
The state must be a valid quantum state vector with dimension 2^n where n is the number of qubits.
Parameters
circuit- The quantum circuitstate- New quantum state vector
Examples
iex> qc = Qx.QuantumCircuit.new(1, 0)
iex> new_state = Nx.tensor([Complex.new(0.0, 0.0), Complex.new(1.0, 0.0)], type: :c64)
iex> qc = Qx.QuantumCircuit.set_state(qc, new_state)
iex> Nx.shape(qc.state)
{2}