Qx.QuantumCircuit (Qx - Quantum Computing Simulator v0.8.0)

View Source

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 current quantum state of 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

instruction()

@type instruction() :: {atom(), list(), list()}

measurement()

@type measurement() :: {integer(), integer()}

t()

@type t() :: %Qx.QuantumCircuit{
  instructions: [instruction()],
  measured_qubits: MapSet.t(),
  measurements: [measurement()],
  num_classical_bits: integer(),
  num_qubits: integer(),
  state: Nx.Tensor.t()
}

Functions

depth(circuit)

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

get_instructions(circuit)

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]}

get_measurements(circuit)

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}

get_state(circuit)

Gets the current 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}

measured?(circuit, qubit)

@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

new(num_qubits)

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

new(num_qubits, num_classical_bits)

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 circuit
  • num_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

reset(circuit)

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

set_state(circuit, state)

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 circuit
  • state - 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}