Skip to content

Dashboard Core — QASM Library

Real, standard OpenQASM 2.0 circuits offered as Composer presets — textbook examples (Bell pair, GHZ, W-state) and dense_evolution's own gate library exercised end to end, not synthetic placeholder text.

import dense_evolution as de
from dashboard_core.qasm_library import gate_tuples_to_qasm

# Any hand-written gate-tuple circuit:
print(gate_tuples_to_qasm([('h', 0), ('cx', 0, 1)], n_qubits=2))

# Or any of dense_evolution's own circuit generators, unmodified:
qft_ops = de.qft(3)
print(gate_tuples_to_qasm(qft_ops, n_qubits=3))

qasm_library

Real, standard OpenQASM 2.0 circuits offered as Composer presets -- textbook examples (Bell pair, GHZ, W-state) and dense_evolution's own real circuit generators (QFT, random circuit), not synthetic results. The Composer runs every one of these through the real engine just like any custom circuit typed in by hand.

gate_tuples_to_qasm

gate_tuples_to_qasm(
    ops, n_qubits: int, measure: bool = True
) -> str

Converts a dense_evolution gate-tuple circuit (the format de.qft/de.ghz_state/de.random_circuit/... all return) into real OpenQASM 2.0 text, so any of dense_evolution's own circuit generators can be offered as a Composer preset without hand-transcribing gates.

Examples:

>>> import dense_evolution as de
>>> from dashboard_core.qasm_library import gate_tuples_to_qasm
>>> gate_tuples_to_qasm([('h', 0), ('cx', 0, 1)], n_qubits=2)
'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\ncreg c[2];\nh q[0];\ncx q[0],q[1];\nmeasure q -> c;\n'
>>> qft_ops = de.qft(3)
>>> print(gate_tuples_to_qasm(qft_ops, n_qubits=3))
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
h q[0];
cp(1.5707963267948966) q[1],q[0];
cp(0.7853981633974483) q[2],q[0];
h q[1];
cp(1.5707963267948966) q[2],q[1];
h q[2];
swap q[0],q[2];
measure q -> c;
Source code in tools/dashboard/core/qasm_library.py
def gate_tuples_to_qasm(ops, n_qubits: int, measure: bool = True) -> str:
    """Converts a dense_evolution gate-tuple circuit (the format
    de.qft/de.ghz_state/de.random_circuit/... all return) into real
    OpenQASM 2.0 text, so any of dense_evolution's own circuit generators
    can be offered as a Composer preset without hand-transcribing gates.

    Examples
    --------
    >>> import dense_evolution as de
    >>> from dashboard_core.qasm_library import gate_tuples_to_qasm
    >>> gate_tuples_to_qasm([('h', 0), ('cx', 0, 1)], n_qubits=2)
    'OPENQASM 2.0;\\ninclude "qelib1.inc";\\nqreg q[2];\\ncreg c[2];\\nh q[0];\\ncx q[0],q[1];\\nmeasure q -> c;\\n'
    >>> qft_ops = de.qft(3)
    >>> print(gate_tuples_to_qasm(qft_ops, n_qubits=3))  # doctest: +NORMALIZE_WHITESPACE
    OPENQASM 2.0;
    include "qelib1.inc";
    qreg q[3];
    creg c[3];
    h q[0];
    cp(1.5707963267948966) q[1],q[0];
    cp(0.7853981633974483) q[2],q[0];
    h q[1];
    cp(1.5707963267948966) q[2],q[1];
    h q[2];
    swap q[0],q[2];
    measure q -> c;
    """
    lines = ['OPENQASM 2.0;', 'include "qelib1.inc";', f'qreg q[{n_qubits}];']
    if measure:
        lines.append(f'creg c[{n_qubits}];')
    for op in ops:
        name = op[0]
        if name in _ONE_QUBIT_STATIC:
            lines.append(f'{name} q[{op[1]}];')
        elif name in _ONE_QUBIT_PARAM:
            lines.append(f'{name}({op[2]}) q[{op[1]}];')
        elif name in _TWO_QUBIT_STATIC:
            lines.append(f'{name} q[{op[1]}],q[{op[2]}];')
        elif name in _TWO_QUBIT_PARAM:
            lines.append(f'{name}({op[3]}) q[{op[1]}],q[{op[2]}];')
        elif name in _THREE_QUBIT_STATIC:
            lines.append(f'{name} q[{op[1]}],q[{op[2]}],q[{op[3]}];')
        else:
            raise ValueError(f"gate_tuples_to_qasm: unrecognized gate {name!r}")
    if measure:
        lines.append('measure q -> c;')
    return '\n'.join(lines) + '\n'

See also: dense_evolution.parser.QASMParser, which these presets are meant to be fed into.