Skip to content

unexpected behavior when generating QASM #10

@plquera

Description

@plquera

The following code, works for the GHZ state prep log depth example:

`def ghz_log_simd(n: int):
n_qubits = int(2**n)

@qasm2.extended
def layer(i_layer: int, qreg: qasm2.QReg):
    step = n_qubits // (2**i_layer)

    def get_qubit(x: int):
        return qreg[x]

    ctrl_qubits = ilist.map(fn=get_qubit, collection=range(0, n_qubits, step))
    targ_qubits = ilist.map(
        fn=get_qubit, collection=ilist.range(step // 2, n_qubits, step)
    )

    # Ry(-pi/2)
    qasm2.parallel.u(qargs=targ_qubits, theta=-math.pi / 2, phi=0.0, lam=0.0)

    # CZ gates
    qasm2.parallel.cz(ctrls=ctrl_qubits, qargs=targ_qubits)

    # Ry(pi/2)
    qasm2.parallel.u(qargs=targ_qubits, theta=math.pi / 2, phi=0.0, lam=0.0)

@qasm2.extended
def ghz_log_depth_program():

    qreg = qasm2.qreg(n_qubits)

    qasm2.h(qreg[0])
    for i in range(n):
        layer(i_layer=i, qreg=qreg)

return ghz_log_depth_program`

But if I try to substitute the Hadamard in the end h(qreg[0]) for a parallelizable u3 gate like in the layer function, things break. This would be the code:

`
def ghz_log_simd(n: int):
n_qubits = int(2**n)

@qasm2.extended
def layer(i_layer: int, qreg: qasm2.QReg):
    step = n_qubits // (2**i_layer)

    def get_qubit(x: int):
        return qreg[x]

    ctrl_qubits = ilist.map(fn=get_qubit, collection=range(0, n_qubits, step))
    targ_qubits = ilist.map(
        fn=get_qubit, collection=ilist.range(step // 2, n_qubits, step)
    )

    # Ry(-pi/2)
    qasm2.parallel.u(qargs=targ_qubits, theta=-math.pi / 2, phi=0.0, lam=0.0)

    # CZ gates
    qasm2.parallel.cz(ctrls=ctrl_qubits, qargs=targ_qubits)

    # Ry(pi/2)
    qasm2.parallel.u(qargs=targ_qubits, theta=math.pi / 2, phi=0.0, lam=0.0)

@qasm2.extended
def ghz_log_depth_program():

    qreg = qasm2.qreg(n_qubits)

    qasm2.parallel.u(qargs=qreg[0], theta=math.pi / 2, phi=0.0, lam=math.pi )
    for i in range(n):
        layer(i_layer=i, qreg=qreg)

return ghz_log_depth_program

`

and this would be the trace error:


ValueError Traceback (most recent call last)
Cell In[61], line 2
1 target = QASM2()
----> 2 ast = target.emit(ghz_log_simd(2))
3 pprint(ast)

File ~/Library/CloudStorage/OneDrive-QuEraComputing/Business Team/Events/2025/YQuantum/bloqade_circuits_work/.bloqade_circuits_env/lib/python3.11/site-packages/bloqade/qasm2/emit/target.py:103, in QASM2.emit(self, entry)
101 Py2QASM(entry.dialects)(entry)
102 target_main = EmitQASM2Main(self.main_target)
--> 103 target_main.run(
104 entry, tuple(ast.Name(name) for name in entry.arg_names[1:])
105 ).expect()
107 main_program = target_main.output
108 assert main_program is not None, f"failed to emit {entry.sym_name}"

File ~/Library/CloudStorage/OneDrive-QuEraComputing/Business Team/Events/2025/YQuantum/bloqade_circuits_work/.bloqade_circuits_env/lib/python3.11/site-packages/kirin/interp/base.py:146, in BaseInterpreter.run(self, mt, args, kwargs)
144 args = self.get_args(mt.arg_names[len(args) + 1 :], args, kwargs)
145 try:
--> 146 _, results = self.run_method(mt, args)
147 except InterpreterError as e:
148 # NOTE: initialize will create new State
149 # so we don't need to copy the frames.
150 return Err(e, self.state.frames)
...
344 name = stmt.dialect.name if stmt.dialect else "None"
--> 345 raise ValueError(f"dialect {name} is not supported by {self.dialects}")
347 return self.eval_stmt_fallback(frame, stmt)

ValueError: dialect qasm2.parallel is not supported by DialectGroup([func, scf, lowering.func, qasm2.indexing, qasm2.uop, qasm2.expr, qasm2.core, lowering.call])

┆Issue is synchronized with this Jira Task by Unito

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingduplicateThis issue or pull request already existsqasm2qasm2 related issues

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions