qBraid / pyqasm

Python toolkit providing OpenQASM 3 semantic analyzer and utilities for program analysis and compilation.
https://sdk.qbraid.com/projects/pyqasm
GNU General Public License v3.0
4 stars 1 forks source link

[FEATURE] `pyqasm.Module`: loads, dumps, and in-place operations for improved UX #50

Open ryanhill1 opened 2 days ago

ryanhill1 commented 2 days ago

Instead of having a large library of individual functions, have two functions loads and dumps which take OpenQASM strings to and from a pyqasm.Module object, and have all supported operations structured as methods that are performed in-place. Or, optionally specify in_place=False when calling any of the methods to return the resulting QASM string without altering the state of the Module object. This would allow chaining operations together without requiring loading a module to perform each op.

Here is a rough example of what the interface could look like:


from pyqasm import loads, dumps

qasm_str = "..."  # Your QASM 3 program as a string

# Load the QASM 3 string into a module
module = loads(qasm_str)

# Print the number of qubits and classical bits in the module
print(module.num_qubits())  # <int>
print(module.num_clbits())  # <int>

# Print the circuit depth
print(module.depth())  # <int>

# Decompose the circuit to a new gate set
module.rebase(basis_gates={"h", "t", "cx"})

# Print the new depth after rebasing
print(module.depth())  # <int>

# Remove barriers from the circuit
module.remove_barriers()

# Draw the circuit using matplotlib (mpl) or other available options
module.draw(output='mpl')  # Options: 'ascii', 'mpl', TBD (e.g., 'latex')

# Dump the module back to a QASM string
qasm_str_out = dumps(module)

# Make a copy of the module
module_2 = module.copy()

# Flatten gates and operations
module_2.unroll()

# Remove measurements, returning a new QASM string (if in_place=False)
qasm_str_2_out = module_2.remove_measurements(in_place=False)  # Default is in_place=True
TheGupta2012 commented 1 day ago

@ryanhill1 I have added some changes in #49 , can you review once before moving ahead? Thanks!