Qx.Step (Qx - Quantum Computing Simulator v0.10.1)
View SourceOne executed operation from Qx.steps/2: what ran, where in the
circuit, and the state right after it.
The struct is raw data. inspect/1 gives you the readable one-liner,
and show/1 a display map (Dirac string, amplitudes, probabilities),
so printing each step of Qx.steps(qc) is already a usable circuit
walkthrough.
Steps come in 3 kinds:
:gate- a unitary gate was applied:measurement- a qubit was measured and the state collapsed;classical_bitsholds the outcome:conditional- a gate inside ac_ifblock;conditionsays which classical bit was tested and whether the block ran
A word on measured circuits: each step carries the state of one
sampled trajectory. After a measurement step the state is collapsed,
so what you see differs from the ensemble probabilities Qx.run/2
reports. That's the point of stepping, but don't confuse the two.
Displaying a step reads all 2^n amplitudes host-side. Cheap at
teaching scale; noticeable when you print steps of a 20-qubit circuit
in a tight loop.
Fields
kind-:gate,:measurement, or:conditionaloperation- the instruction just applied, as{gate, qubits, params};nilfor a not-taken conditional blockindex- 0-based position in the executed timelinestate- the statevector after this step (:c64tensor, same shapeQx.get_state/1returns)probabilities-Qx.Math.probabilities/1of that stateclassical_bits- classical register contents so far (defaults to[]for circuits without measurements)condition-nil, or{cbit, value, :taken | :not_taken}on conditional steps
Summary
Functions
Returns the display map for a step's state: a Dirac string plus per-basis amplitudes and probabilities.
Types
@type condition() :: {non_neg_integer(), 0 | 1, :taken | :not_taken}
@type kind() :: :gate | :measurement | :conditional
@type operation() :: {atom(), [non_neg_integer()], [number()]}
@type t() :: %Qx.Step{ classical_bits: [0 | 1], condition: condition() | nil, index: non_neg_integer() | nil, kind: kind() | nil, operation: operation() | nil, probabilities: Nx.Tensor.t() | nil, state: Nx.Tensor.t() | nil }
Functions
Returns the display map for a step's state: a Dirac string plus per-basis amplitudes and probabilities.
The shape is
%{state: dirac, amplitudes: [{basis, "a+bi"}], probabilities: [{basis, p}]}.
For steps after a measurement the Dirac string shows the collapsed state of that sampled trajectory.
Examples
iex> qc = Qx.create_circuit(2) |> Qx.h(0) |> Qx.cx(0, 1)
iex> state = Qx.Simulation.get_state(qc)
iex> step = %Qx.Step{kind: :gate, operation: {:cx, [0, 1], []}, index: 1,
...> state: state, probabilities: Qx.Math.probabilities(state)}
iex> Qx.Step.show(step).state
"0.707|00⟩ + 0.707|11⟩"