uniqc.simulator.opcode_simulator module

Opcode simulator, a fundamental simulator for UnifiedQuantum. It simulates from a basic opcode

class uniqc.simulator.opcode_simulator.OpcodeSimulator(backend_type='statevector')[source]

Bases: object

A quantum circuit simulator based on C++ that runs locally.

Parameters:

backend_type – Backend type for simulation. Supported: ‘statevector’, ‘density_matrix’, ‘density_matrix_qutip’. Defaults to ‘statevector’.

Variables:

simulator – The underlying C++ simulator instance. typestr: Human-readable backend type string.

creg_value()[source]

Return the whole CREG as an integer with c[0] as the LSB.

get_cbit(index)[source]

Return CREG bit index (0/1).

init_creg(n_cbit)[source]

(Re)initialize the CREG store to n_cbit zeroed single-bit cells.

register_qram(name, addr_size, data_size, data_array)[source]

Register a QRAM data array for simulation.

Parameters:
  • name – QRAM identifier (matches the operation name in opcodes).

  • addr_size – Number of address qubits.

  • data_size – Number of data qubits.

  • data_array – Reference to the data list (shared with the QRAM object).

set_cbit(index, value)[source]

Write value (coerced to 0/1) into CREG bit index.

simulate_classical(node)[source]

Execute a ClassicalOp-like node against the CREG store: read its source operands, compute the result, and write it to the destination bit. Returns the written value (0/1).

simulate_gate(operation, qubit, cbit, parameter, is_dagger, control_qubits_set)[source]

Apply a single gate opcode to the simulator.

Parameters:
  • operation – Gate name string.

  • qubit – Qubit index or list of indices.

  • cbit – Classical bit index.

  • parameter – Gate parameters.

  • is_dagger – Whether to apply the dagger version of the gate.

  • control_qubits_set – Set of control qubits.

simulate_measure(qubit, cbit)[source]

Measure qubit (collapsing the state) and store the outcome in CREG bit cbit. Returns the measured outcome (0/1).

simulate_opcodes_density_operator(n_qubit, program_body)[source]

Compute the density matrix after executing a list of opcodes.

Parameters:
  • n_qubit – Number of qubits.

  • program_body – List of opcodes to simulate.

Returns:

Density matrix as a 2D numpy array.

Raises:

ValueError – If simulator type is unknown.

simulate_opcodes_pmeasure(n_qubit, program_body, measure_qubits)[source]

Compute measurement probabilities for a list of opcodes.

Parameters:
  • n_qubit – Number of qubits.

  • program_body – List of opcodes to simulate.

  • measure_qubits – Qubits to measure.

Returns:

List of probabilities for each measurement outcome.

simulate_opcodes_shot(n_qubit, program_body, measure_qubits)[source]

Execute a list of opcodes and return a single measurement sample.

Parameters:
  • n_qubit – Number of qubits.

  • program_body – List of opcodes to simulate.

  • measure_qubits – Qubits to measure.

Returns:

Integer representing the measured bitstring (decimal).

Raises:

NotImplementedError – If backend is density_operator type.

simulate_opcodes_stateprob(n_qubit, program_body)[source]

Compute state probabilities (|amplitude|^2) for all basis states.

Parameters:
  • n_qubit – Number of qubits.

  • program_body – List of opcodes to simulate.

Returns:

Array of probabilities for each basis state.

Raises:

ValueError – If simulator type is unknown.

simulate_opcodes_statevector(n_qubit, program_body)[source]

Compute the final statevector after executing a list of opcodes.

Parameters:
  • n_qubit – Number of qubits.

  • program_body – List of opcodes to simulate.

Returns:

Statevector as a complex numpy array.

Raises:

ValueError – If backend is density_matrix type.

simulate_reset(qubit)[source]

Reset qubit to |0>.

uniqc.simulator.opcode_simulator.backend_alias(backend_type)[source]

Resolve backend type aliases to canonical names.

Recognised aliases (case-insensitive):

statevector / state_vector                   -> statevector
density_matrix / density_operator
    / densitymatrix / densityoperator        -> density_operator
density_matrix_qutip / density_operator_qutip
                                             -> density_operator_qutip
Returns:

Canonical backend type string (“statevector”, “density_operator”, or “density_operator_qutip”).

Raises:

ValueError – If backend_type is not a recognised alias.