qolumbina.utils.execution_framework module#
The top-level interface for quantum system simulation. The current version has the following limitations:
Only the quantum programs executing a unitary operation \(U\) are supported. This means that the circuit under test cannot include classical bits or mid-circuit measurements.
Parameterized quantum gates are not supported for state initialization, such as \(R_X(\theta)\) being excluded.
The measurement basis is currently limited to Pauli bases (\(X\), \(Y\), \(Z\)) or identity (\(I\), meaning no measurement).
These constraints are planned to be relaxed in the future development.
- class AnalyticIdealSimulation(tested_subroutine, init_state=None, init_qubits='all', backend='statevector', target_qubits=None)[source]#
Bases:
objectExecute a quantum program on an analytic backend without quantum measurements.
- Parameters:
tested_subroutine (Callable[[], QuantumCircuit]) – A callable that constructs and returns the QuantumCircuit under test.
init_state (list | None) –
Initial state specification for all qubits. If None, all qubits are initialized to \(\ket{0}\).
See also
qolumbina.utils.state_initialization module for input types for state initialization.
init_qubits (Literal['all'] | list[int] | None) –
Specifies which qubits to allocate and initialize according to init_state. The default value is “all”. Use
Noneto skip explicit initialization entirely; this is only valid wheninit_stateis alsoNone."all":All qubits are initialized. This means that the number of qubits for the entire circuit is determined by init_state.
list[int]:Only the qubits with the specified indices are initialized according to init_state. In this case, the number of qubits for the entire circuit automatically matches that of the circuit under test.
None:No qubits are explicitly initialized. The system qubits keep the default \(\ket{0}\) state. This cannot be combined with
init_state.
Example
init_qubits=[0, 2]means that only qubits with indices 0 and 2 are initialized according toinit_state, while other qubits are initialized to \(\ket{0}\) by default.backend (str) – Backend name for circuit execution. Supported analytic backends include
"statevector","density_matrix", and"unitary".target_qubits (list[int] | None) – Optional list of qubit indices for which the analytic result is extracted. This is only applicable for the state vector backend. If None, the full state vector is returned.
Input validation and errors
The following cases are checked before or during input-state preparation.
Explicitly skipping initialization:
init_qubits=Nonemeans no qubits are explicitly initialized. It is valid only wheninit_state is None; otherwiseValueErroris raised, becauseinit_statewould have no target qubits to initialize.Pure-state initialization: When
init_qubitsis"all"orlist[int], validation is delegated toPureStateInitialization. It may raiseTypeErrorwheninit_stateis not a supported list orinit_qubitsis neither"all"norlist[int]. It may raiseValueErrorwheninit_stateis empty, contains an undefined single-qubit gate, has a gate-list length that does not matchinit_qubits, has a state-vector dimension that does not match the selected qubits, or violates the generated circuit-width constraints.
Note
Regarding the test output, we can access the following attributes after execution:
- self.entire_qc (QuantumCircuit)
The full QuantumCircuit executed (including initialization, tested subroutine).
- self.test_output (Any)
An analytic result, which could be a state vector, density matrix, or unitary matrix.
- property init_qubits: Literal['all'] | list[int] | None#
- property init_state: list#
- class QuantumSystemSimulation(tested_subroutine, init_state=None, init_qubits='all', input_mode='pure', meas_mapping=None, meas_basis=None, shots=1024, backend='ideal', noise_model=None, if_noisy=False, random_seed=None, mixed_state_angles=None)[source]#
Bases:
objectExecute a quantum program on a shot-based simulated quantum computer.
This class builds an execution circuit around a circuit under test:
prepare an input state on selected system qubits;
compose the quantum circuit returned by
tested_subroutine;apply Pauli-basis measurements;
execute the complete circuit on the selected backend.
The circuit returned by
tested_subroutinedefines the system-qubit register. In pure-state mode,init_qubitsmay select only a subset of those system qubits for explicit input-state preparation; all other system qubits remain in \(\ket{0}\). In mixed-state mode, the selected system qubits are prepared through appended control/environment qubits, while the circuit under test still acts only on the original system qubits.- Parameters:
tested_subroutine (Callable[[], QuantumCircuit]) – A callable that constructs and returns the QuantumCircuit under test.
init_state (list[str] | list[float] | list[complex] | None) –
Initial state specification for the selected input qubits. If
None, the selected input qubits are initialized to \(\ket{0}\).In pure-state mode, the meaning of
init_statedepends oninit_qubits. Ifinit_qubits="all",init_statedescribes every system qubit. Ifinit_qubitsis a list,init_statedescribes only those listed qubits, matched by position. For example, for a 5-qubit circuit, usinginit_state=["x", "x"]andinit_qubits=[0, 1]prepares only system qubitsq0andq1as \(\ket{11}\); the remaining system qubitsq2,q3, andq4remain \(\ket{0}\) unless changed by the circuit under test.See also
qolumbina.utils.state_initialization module for input types for state initialization.
init_qubits (Literal['all'] | list[int] | None) –
Specifies which system qubits need input-state preparation. The default value is “all”. Use
Noneto skip explicit input-state preparation; this is only valid wheninit_stateis alsoNone."all":Pure mode: all qubits are initialized according to
init_state. Mixed mode: ifmixed_state_anglesis provided, the targets default to the lowmsystem qubits, wheremis the number of angles; otherwise all system qubits are mixed-state targets.
list[int]:Pure mode: only these qubits are initialized according to
init_state. Mixed mode: these are the system target qubits for mixed-state preparation.
None:No qubits are explicitly initialized. The system qubits keep the default \(\ket{0}\) state. This cannot be combined with
init_state.
Example
init_qubits=[0, 2]means that only qubits with indices 0 and 2 are prepared, while other system qubits remain initialized to \(\ket{0}\) by default.input_mode (Literal['pure', 'mixed']) – Input-state preparation mode.
"pure"preserves the originalinit_state/init_qubitsbehavior."mixed"uses quantum-control qubits andmixed_state_anglesto prepare separable mixed states on the system qubits selected byinit_qubits.meas_mapping (dict[int, int] | None) –
Measurement mapping from qubit index to classical bit index, which impacts the analysis of test output. Default value is
None. If bothmeas_mappingandmeas_basisareNone, every default measured system qubit is measured in Pauli-Z basis and mapped correspondingly, meaning that qubit with index \(i\) is measured to classical bit with index \(i\). Ifmeas_mappingis provided whilemeas_basisisNone, only the qubits inmeas_mappingare measured, all in Pauli-Z basis. In mixed-state mode, appended control/environment qubits are internal to input preparation and are not included in this default output mapping.Example
meas_mapping={0: 1, 2: 0}means that qubit 0 is measured to classical bit 1, while qubit 2 is measured to classical bit 0.See also
qolumbina.utils.QuantumMeasurementandqolumbina.utils.QuantumMeasurement.measure()for the complete measurement-resolution rules and examples that controlmeas_mappingindependently frommeas_basis.meas_basis (list[str] | dict[int, str] | None) –
Measurement basis. Can be
"x","y","z"or"i"(identity, meaning no measurement). Alist[str]is dense and must cover all qubits in the circuit being measured. Adict[int, str]is sparse: its keys are qubit indices and unspecified qubits are treated as"i". Default value isNone. If bothmeas_basisandmeas_mappingareNone, all default measured system qubits are measured in Pauli-Z basis. In pure-state mode this means every system qubit in the circuit under test. In mixed-state mode this means every original system qubit, not the appended control/environment qubits.Example
meas_basis=["x", "z", "y"]means that qubit 0 is measured in Pauli-X basis, qubit 1 is measured in Pauli-Z basis, and qubit 2 is measured in Pauli-Y basis.To measure only a subset of qubits, either use
"i"for qubits that should not be measured, or provide a sparse dict. Whenmeas_mappingis omitted, classical bits are assigned contiguously in ascending qubit-index order. For example, for a 5-qubit circuit,meas_basis=["z", "z", "i", "i", "i"]ormeas_basis={0: "z", 1: "z"}measures onlyq0andq1and maps them toc0andc1. If bothmeas_basisandmeas_mappingare provided, their measured qubit sets must match exactly; otherwise aValueErroris raised.See also
qolumbina.utils.QuantumMeasurementandqolumbina.utils.QuantumMeasurement.measure()for the complete measurement-resolution rules and examples that controlmeas_basisindependently frommeas_mapping.shots (int) – Number of shots per circuit execution. Default value is 1024.
backend (str) –
Backend name for circuit execution.
See also
qolumbina.utils.backend_execution module for supported backends.
noise_model (NoiseModel | None) – Optional custom NoiseModel.
if_noisy (bool) – Whether to enable noise when using fake backend.
random_seed (int | None) – Optional random seed for simulator to ensure reproducibility.
mixed_state_angles (list[float] | None) –
Optional Ry angles for separable quantum-control qubits used to prepare a mixed-state input when
input_mode="mixed". Each angle prepares one extra control qubit asRy(theta)|0>and then controls one target system qubit. IfNonein mixed mode, all target angles default topi / 2, which is equivalent to Hadamard probabilities.In mixed-state mode, system qubits keep their original indices
0..n-1. Target qubits are selected byinit_qubitsand can be any distinct valid system-qubit indices. Control qubits are appended after the maximum system-qubit index, namelyn..n+m-1. Control qubits are not measured into the test output; they only act as an environment/purification for the target system qubits.Example
To prepare the single-qubit mixed state \(\rho = \cos^2(\theta / 2)\lvert 0\rangle\langle 0\rvert + \sin^2(\theta / 2)\lvert 1\rangle\langle 1\rvert\) on system qubit
0, use one appended control qubit prepared byRy(theta)and entangled with the target:sim = QuantumSystemSimulation( tested_subroutine=lambda: QuantumCircuit(1), input_mode="mixed", mixed_state_angles=[theta], init_qubits=[0], shots=1024, backend="ideal", )
For a two-qubit separable mixed input such as \(\rho = \rho_0 \otimes \rho_2\) on non-contiguous system qubits
0and2, provide one angle per target qubit:sim = QuantumSystemSimulation( tested_subroutine=lambda: QuantumCircuit(3), input_mode="mixed", mixed_state_angles=[theta_0, theta_2], init_qubits=[0, 2], shots=1024, backend="ideal", )
Mode-specific defaults
input_mode="pure"Pure-state mode is the default mode. The input-related defaults are:
init_state=None: initialize the selected system qubits to \(\ket{0}\).init_qubits="all": initialize every system qubit according toinit_state.init_qubits=None: skip explicit input-state preparation; all system qubits remain in their default \(\ket{0}\) state.mixed_state_angles=None: no mixed-state preparation is used.
In this mode,
mixed_state_anglesmust remainNone. The resolved prepared system qubits are stored inself.input_preparation_qubits.input_mode="mixed"Mixed-state mode prepares selected system qubits through appended quantum-control qubits. The input-related defaults are:
init_state=None: required. System targets are prepared by mixed-state preparation, not by the pure-state initializer.init_qubits="all"andmixed_state_angles=None: all system qubits are mixed-state targets and all angles default topi / 2.init_qubits="all"andmixed_state_anglesis a list of lengthm: the mixed-state targets default to low-index system qubits[0, 1, ..., m - 1].init_qubitsislist[int]andmixed_state_angles=None: the listed qubits are mixed-state targets and all angles default topi / 2.init_qubitsislist[int]andmixed_state_anglesis a list: the two lists are matched position-wise, somixed_state_angles[i]preparesinit_qubits[i].
In this mode, the resolved target system qubits are stored in
self.input_preparation_qubitsand the appended control qubits are stored inself.mixed_state_control_qubits. Control qubits are not measured intoself.test_output.
Measurement output and bit-string order
self.test_outputstores the shot counts returned by Qiskit, so the keys follow Qiskit’s classical-bit string convention. The leftmost character corresponds to the highest displayed classical bit and the rightmost character corresponds to classical bit 0.With the default measurement mapping
q_i -> c_i, this means that a 5-qubit circuit whose measured state isq4 q3 q2 q1 q0 = 0 0 0 1 1appears inself.test_outputas"00011", not"11000". This convention matters when writing expected outputs for tests: the bit string is displayed in classical register order, while qubit indices are commonly discussed from low to high.Example:
sim = QuantumSystemSimulation( tested_subroutine=lambda: QuantumCircuit(5), init_state=["x", "x"], init_qubits=[0, 1], meas_basis=None, shots=1024, backend="ideal", ) # Empty circuit under test; q0 and q1 are one, q2..q4 are zero. # The expected count key is "00011" under the default q_i -> c_i # measurement mapping. assert sim.test_output == {"00011": 1024}
If only low-index qubits are relevant, measure only those qubits to make the output width explicit:
sim = QuantumSystemSimulation( tested_subroutine=lambda: QuantumCircuit(5), init_state=["x", "x"], init_qubits=[0, 1], meas_basis=["z", "z", "i", "i", "i"], meas_mapping={0: 0, 1: 1}, shots=1024, backend="ideal", ) assert sim.test_output == {"11": 1024}
Input validation and errors
The following cases are checked before or during input-state preparation. They are grouped by configuration area so callers can map an exception back to the invalid input quickly.
Mode selection:
ValueErroris raised ifinput_modeis not"pure"or"mixed".Explicitly skipping initialization:
init_qubits=Nonemeans no qubits are explicitly initialized. It is valid only wheninit_state is Noneandmixed_state_angles is None. OtherwiseValueErroris raised, because those inputs would have no target qubits to initialize.Pure-state mode:
mixed_state_anglesis invalid wheninput_mode="pure"and raisesValueError. If pure-state preparation is used, the delegatedPureStateInitializationvalidation may also raise:TypeErrorwheninit_stateis not a supported list orinit_qubitsis neither"all"norlist[int];ValueErrorwheninit_stateis empty, contains an undefined single-qubit gate, has a gate-list length that does not matchinit_qubits, has a state-vector dimension that does not match the selected qubits, or violates the generated circuit-width constraints.Mixed-state mode:
init_stateis invalid wheninput_mode="mixed"and raisesValueError. Mixed-state target resolution may also raise:ValueErrorwhenmixed_state_anglesis provided but is not a non-empty list, wheninit_qubitsis an empty list, when the angle list and explicit target-qubit list have different lengths, when target qubits are duplicated, or when any target qubit is outside the system-qubit range;TypeErrorwhenmixed_state_anglescontains non-real values orinit_qubitscontains non-integer values.
Note
Regarding the test output, we can access the following attributes after execution:
- self.entire_qc (QuantumCircuit)
The full QuantumCircuit executed (including initialization, tested subroutine, measurement).
- self.transpiled_qc (QuantumCircuit)
The transpiled circuit under test, excluding state initialization and measurement. In pure-state mode, explicit state-preparation gates are excluded. In mixed-state mode, this is the system circuit only.
- The following properties can be accessed for the transpiled circuit:
transpiled_cut_width,transpiled_cut_size,transpiled_cut_depth.
- self.transpiled_execution_qc (QuantumCircuit)
The full transpiled QuantumCircuit executed on the backend, including initialization, tested subroutine, control qubits for mixed-state preparation, and measurement operations.
The following properties can be accessed for the full execution circuit:
transpiled_execution_width,transpiled_execution_size,transpiled_execution_depth.- self.test_output (dict[str, int])
Measurement counts as a dictionary. e.g.,
{'00': 512, '11': 512}
- property has_mixed_state_configuration: bool#
- property init_qubits: Literal['all'] | list[int] | None#
- property init_state: list#
- property meas_basis: list[str] | dict[int, str]#
- property meas_mapping: dict[int, int]#
- property transpiled_cut_depth: int#
Depth of the quantum-gate DAG (a.k.a. depth) of the transpiled circuit under test.
Compared to
qc.depthforqcwithQuantumCircuittype, only quantum gates are counted for the depth calculation, which should exclude measurement and barrier.
- property transpiled_cut_size: int#
Number of quantum gates (a.k.a. size) of the transpiled circuit under test.
Compared to
qc.sizeforqcwithQuantumCircuittype, only quantum gates are counted, meaning that measurement and barrier are excluded.
- property transpiled_cut_width: int#
Number of qubits (a.k.a. width) of the transpiled circuit under test.
Compared to
qc.widthforqcwithQuantumCircuittype, classical bits are excluded.
- property transpiled_execution_depth: int#
Quantum-gate depth of the full transpiled execution circuit.
Measurement and barrier operations are excluded, but initialization, mixed-state preparation, basis-change gates, and the circuit under test are included.
- property transpiled_execution_size: int#
Number of quantum gates of the full transpiled execution circuit.
Measurement and barrier operations are excluded, but initialization, mixed-state preparation, basis-change gates, and the circuit under test are included.
- property transpiled_execution_width: int#
Number of qubits of the full transpiled execution circuit.
In mixed-state mode, this includes both system qubits and appended control/environment qubits.