uniqc.circuit_builder.random_qasm module¶
Random OpenQASM 2.0 circuit generator.
This module provides functions for generating random quantum circuits in OpenQASM 2.0 format. It supports random gate selection, measurement generation, and circuit construction from opcode lists.
- Key exports:
build_qasm_gate: Build a single QASM gate string. build_full_measurements: Generate measurement instructions for all qubits. build_measurements: Generate measurement instructions for specified qubits. random_qasm: Generate a complete random QASM program. build_qasm_from_opcodes: Convert a list of opcodes to QASM code.
- uniqc.circuit_builder.random_qasm.build_full_measurements(n_qubits, qreg_name='q', creg_name='c')[source]¶
Build a QASM string that measures all qubits in the given qreg to the given creg.
- Parameters:
n_qubits (int) – number of qubits to measure
qreg_name (str) – name of the qreg to measure from
creg_name (str) – name of the creg to measure to
- Returns:
a list of QASM strings that measure all qubits in the given qreg to the given creg.
- Return type:
List[str]
- uniqc.circuit_builder.random_qasm.build_measurements(measure_qbit_cbit_pairs, qreg_name='q', creg_name='c')[source]¶
Build a QASM string that measures specified qubits to classical bits.
- Parameters:
measure_qbit_cbit_pairs – Iterable of (qubit_index, cbit_index) tuples.
qreg_name (str) – name of the quantum register.
creg_name (str) – name of the classical register.
- Returns:
a list of QASM measurement instructions.
- Return type:
List[str]
- uniqc.circuit_builder.random_qasm.build_qasm_from_opcodes(opcode_list, measure_qbit_cbit=None, qreg_name='q', creg_name='c')[source]¶
Generate a QASM code from a list of opcodes.
- Parameters:
opcode_list (List[Tuple[str, List[int], List[float]]]) – a list of tuples containing the gate name, qubits, and parameters
measure_qbit_cbit (List[Tuple[int, int]]) – a list of tuples containing the qubit and cbit to measure
qreg_name (str) – name of the qreg to measure from
creg_name (str) – name of the creg to measure to
- Returns:
a QASM code
- Return type:
str
- uniqc.circuit_builder.random_qasm.build_qasm_gate(gate, qubits, params, qreg_name='q')[source]¶
Build a QASM gate string with the given gate, qubits, and parameters.
- Parameters:
gate (str) – name of the gate
qubits (List[int]) – list of qubits the gate acts on
params (List[float]) – list of parameters of the gate
- Returns:
a QASM gate string
- Return type:
str
- uniqc.circuit_builder.random_qasm.random_qasm(n_qubits, n_gates, instruction_set={'c3x': {'params': 0, 'qubit': 4}, 'ccx': {'params': 0, 'qubit': 3}, 'crx': {'params': 1, 'qubit': 2}, 'cry': {'params': 1, 'qubit': 2}, 'crz': {'params': 1, 'qubit': 2}, 'cswap': {'params': 0, 'qubit': 3}, 'cu1': {'params': 1, 'qubit': 2}, 'cu3': {'params': 3, 'qubit': 2}, 'cx': {'params': 0, 'qubit': 2}, 'cy': {'params': 0, 'qubit': 2}, 'cz': {'params': 0, 'qubit': 2}, 'h': {'params': 0, 'qubit': 1}, 'id': {'params': 0, 'qubit': 1}, 'iswap': {'params': 0, 'qubit': 2}, 'p': {'params': 1, 'qubit': 1}, 'phase2q': {'params': 3, 'qubit': 2}, 'rphi': {'params': 2, 'qubit': 1}, 'rx': {'params': 1, 'qubit': 1}, 'rxx': {'params': 1, 'qubit': 2}, 'ry': {'params': 1, 'qubit': 1}, 'ryy': {'params': 1, 'qubit': 2}, 'rz': {'params': 1, 'qubit': 1}, 'rzz': {'params': 1, 'qubit': 2}, 's': {'params': 0, 'qubit': 1}, 'sdg': {'params': 0, 'qubit': 1}, 'swap': {'params': 0, 'qubit': 2}, 'sx': {'params': 0, 'qubit': 1}, 'sxdg': {'params': 0, 'qubit': 1}, 't': {'params': 0, 'qubit': 1}, 'tdg': {'params': 0, 'qubit': 1}, 'u': {'params': 3, 'qubit': 1}, 'u1': {'params': 1, 'qubit': 1}, 'u2': {'params': 2, 'qubit': 1}, 'u3': {'params': 3, 'qubit': 1}, 'uu15': {'params': 15, 'qubit': 2}, 'x': {'params': 0, 'qubit': 1}, 'xy': {'params': 1, 'qubit': 2}, 'y': {'params': 0, 'qubit': 1}, 'z': {'params': 0, 'qubit': 1}}, measurements=True)[source]¶
Generate a random QASM code with n_qubits and n_gates from the given instruction set.
- Parameters:
n_qubits (int) – number of qubits in the circuit
n_gates (int) – number of gates in the circuit
instruction_set (Dict) – list of valid QASM instructions
- Returns:
a random QASM code
- Return type:
str