oqpy

raw JSON →
0.3.10 verified Mon Apr 27 auth: no python

oqpy is a Python library for generating OpenQASM 3 + OpenPulse programs programmatically. Current version is 0.3.10, with a monthly-to-quarterly release cadence.

pip install oqpy
error AttributeError: module 'oqpy' has no attribute 'Program'
cause Incorrect import: many examples online use `from oqpy.program import Program` but the correct import is `from oqpy import Program`.
fix
Use from oqpy import Program.
error TypeError: extern() missing 1 required positional argument: 'return_type'
cause In oqpy 0.3.5+, `extern()` requires a `return_type` argument.
fix
Add return_type to extern declaration, e.g., program.extern('my_extern', return_type=oqpy.FloatVar).
error ValueError: Cannot convert value 0 to an OQPY duration
cause Using plain integers/float where a duration type is expected.
fix
Wrap the value in DurationLiteral, e.g., oqpy.DurationLiteral(0, 'ns').
breaking In v0.3.0, the API for declaring variables changed. Use `program.declare_qubit()` instead of `program.qubit()`.
fix Update to use `declare_qubit`, `declare_bit`, `declare_float`, etc.
deprecated The `extern` function signature changed; now requires `return_type` parameter.
fix Add `return_type` argument to `program.extern()` calls.
gotcha When using array indexing, results in oqpy types are not automatically converted to Python slices. Use `oqpy.slice()` to avoid bugs.
fix Use `oqpy.slice(start, stop, step)` instead of Python's built-in slice.
gotcha Duration literals must be constructed with `oqpy.DurationLiteral(value, unit)`; plain numbers are not automatically converted.
fix Always use `DurationLiteral` for duration values.

Create a simple OpenQASM 3 program with two qubits, apply Hadamard, and measure.

from oqpy import Program

# Create a quantum program
program = Program()
# Declare a qubit register
q = program.declare_qubit('q', size=2)
# Add a gate
program.gate("h", q[0])
# Measure
program.measure(q[0], program.declare_bit('c'))
# Output OpenQASM 3
print(program.to_qasm())