Qx.QuantumCircuit (Qx - Quantum Computing Simulator v0.5.0)
View SourceFunctions 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
Adds a single-qubit gate instruction to the circuit.
Adds a measurement instruction to the circuit.
Adds a three-qubit gate instruction to the circuit.
Adds a two-qubit gate instruction to the circuit.
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
@type t() :: %Qx.QuantumCircuit{ instructions: [instruction()], measured_qubits: MapSet.t(), measurements: [measurement()], num_classical_bits: integer(), num_qubits: integer(), state: Nx.Tensor.t() }
Functions
Adds a single-qubit gate instruction to the circuit.
Parameters
circuit- The quantum circuitgate_name- Name of the gate (e.g., :h, :x, :y, :z)qubit- Target qubit indexparams- Optional gate parameters (default: [])
Examples
iex> qc = Qx.QuantumCircuit.new(2, 0)
iex> qc = Qx.QuantumCircuit.add_gate(qc, :h, 0)
iex> length(qc.instructions)
1
Adds a measurement instruction to the circuit.
Parameters
circuit- The quantum circuitqubit- Qubit index to measureclassical_bit- Classical bit index to store the result
Examples
iex> qc = Qx.QuantumCircuit.new(2, 2)
iex> qc = Qx.QuantumCircuit.add_measurement(qc, 0, 0)
iex> length(qc.measurements)
1
Adds a three-qubit gate instruction to the circuit.
Parameters
circuit- The quantum circuitgate_name- Name of the gate (e.g., :ccx)control1- First control qubit indexcontrol2- Second control qubit indextarget- Target qubit indexparams- Optional gate parameters (default: [])
Examples
iex> qc = Qx.QuantumCircuit.new(3, 0)
iex> qc = Qx.QuantumCircuit.add_three_qubit_gate(qc, :ccx, 0, 1, 2)
iex> length(qc.instructions)
1
Adds a two-qubit gate instruction to the circuit.
Parameters
circuit- The quantum circuitgate_name- Name of the gate (e.g., :cx, :cz)control_qubit- Control qubit indextarget_qubit- Target qubit indexparams- Optional gate parameters (default: [])
Examples
iex> qc = Qx.QuantumCircuit.new(2, 0)
iex> qc = Qx.QuantumCircuit.add_two_qubit_gate(qc, :cx, 0, 1)
iex> length(qc.instructions)
1
Gets the depth (number of instruction layers) of the circuit.
Examples
iex> qc = Qx.QuantumCircuit.new(2, 0)
iex> qc = qc |> Qx.QuantumCircuit.add_gate(:h, 0) |> Qx.QuantumCircuit.add_gate(:x, 1)
iex> Qx.QuantumCircuit.depth(qc)
2
Gets the list of instructions in the circuit.
Examples
iex> qc = Qx.QuantumCircuit.new(2, 0)
iex> qc = Qx.QuantumCircuit.add_gate(qc, :h, 0)
iex> [{gate_name, qubits, params}] = Qx.QuantumCircuit.get_instructions(qc)
iex> gate_name
:h
iex> qubits
[0]
Gets the list of measurements in the circuit.
Examples
iex> qc = Qx.QuantumCircuit.new(2, 2)
iex> qc = Qx.QuantumCircuit.add_measurement(qc, 0, 0)
iex> [{qubit, classical_bit}] = Qx.QuantumCircuit.get_measurements(qc)
iex> qubit
0
iex> classical_bit
0
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}
@spec measured?(t(), non_neg_integer()) :: boolean()
Checks if a qubit has been measured.
Examples
iex> qc = Qx.QuantumCircuit.new(2, 2)
iex> qc = Qx.QuantumCircuit.add_measurement(qc, 0, 0)
iex> Qx.QuantumCircuit.measured?(qc, 0)
true
iex> Qx.QuantumCircuit.measured?(qc, 1)
false
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
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.
Examples
iex> qc = Qx.QuantumCircuit.new(2, 2)
iex> qc = qc |> Qx.QuantumCircuit.add_gate(:h, 0) |> Qx.QuantumCircuit.add_measurement(0, 0)
iex> qc_reset = Qx.QuantumCircuit.reset(qc)
iex> length(qc_reset.instructions)
0
iex> length(qc_reset.measurements)
0
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}