Skip to content

Wrapped qasm gate definition throws QasmException #7007

@bharat-thotakura

Description

@bharat-thotakura

Description of the issue
Passing an OpenQASM 2.0 string like:

OPENQASM 2.0;
include "qelib1.inc";
gate gate_PauliEvolution(param0) q0,q1,q2,q3 { rzz(8.227302641391761) q2,q3; rzz(8.227302641391761) q0,q1; rzz(8.227302641391761) q0,q3; }
gate gate_PauliEvolution_140345447972352(param0) q0,q1,q2,q3 { rx(4.508626411417277) q3; rx(4.508626411417277) q2; rx(4.508626411417277) q1; rx(4.508626411417277) q0; }
gate gate_PauliEvolution_140344494262464(param0) q0,q1,q2,q3 { rzz(2.935954138718162) q2,q3; rzz(2.935954138718162) q0,q1; rzz(2.935954138718162) q0,q3; }
gate gate_PauliEvolution_140344494417552(param0) q0,q1,q2,q3 { rx(1.865563952607601) q3; rx(1.865563952607601) q2; rx(1.865563952607601) q1; rx(1.865563952607601) q0; }
gate gate_QAOA(param0,param1,param2,param3) q0,q1,q2,q3 { h q0; h q1; h q2; h q3; gate_PauliEvolution(4.1136513206958805) q0,q1,q2,q3; gate_PauliEvolution_140345447972352(2.2543132057086384) q0,q1,q2,q3; gate_PauliEvolution_140344494262464(1.467977069359081) q0,q1,q2,q3; gate_PauliEvolution_140344494417552(0.9327819763038006) q0,q1,q2,q3; }
qreg q[4];
gate_QAOA(4.1136513206958805,2.2543132057086384,1.467977069359081,0.9327819763038006) q[0],q[1],q[2],q[3];

to circuit_from_qasm() throws:

QasmException: Syntax error: 'gate'
...gate gate_PauliEvolution(param0) q0,q1,q2,q3 { rzz(8.227302641391761) q2,q3; rzz(8.227302641391761) q0,q1; rzz(8.227302641391761) q0,q3; }
gate gate_PauliEvolution_140343572895824(param0) q0,q1,q2,q3 { rx(4.508626411417277) q3; rx(4.508626411417277) q2; rx(4.508626411417277) q1; rx(4.508626411417277) q0; }
gate gate_PauliEvolution_140343573297872(param0) q0,q1,q2,q3 { rzz(2.935954138718162) q2,q3; rzz(2.935954138718162) q0,q1; rzz(2.935954138718162) q0,q3; }
gate gate_PauliEvolution_140343555602864(param0) q0,q1,q2,q3 { rx(1.865563952607601) q3; rx(1.865563952607601) q2; rx(1.865563952607601) q1; rx(1.865563952607601) q0; }
gate gate_QAOA(param0,param1,param2,param3) q0,q1,q2,q3 { h q0; h q1; h q2; h q3; gate_PauliEvolution(4.1136513206958805) q0,q1,q2,q3; gate_PauliEvolution_140343572895824(2.2543132057086384) q0,q1,q2,q3; gate_PauliEvolution_140343573297872(1.467977069359081) q0,q1,q2,q3; gate_PauliEvolution_140343555602864(0.9327819763038006) q0,q1,q2,q3; }
qreg q[4];
gate_QAOA(4.1136513206958805,2.2543132057086384,1.467977069359081,0.9327819763038006) q[0],q[1],q[2],q[3]
        ^
at line 3, column 6

How to reproduce the issue
This can be reproduced via the following code snippet:

from cirq.contrib.qasm_import import circuit_from_qasm
import numpy as np
import qiskit
from qiskit import qasm2
from qiskit.circuit.library import QAOAAnsatz
from qiskit.quantum_info import SparsePauliOp

depth = 2
cost_operator = SparsePauliOp(["ZZII", "IIZZ", "ZIIZ"])
qaoa_circuit = QAOAAnsatz(cost_operator, reps=depth)
main_circuit = qiskit.QuantumCircuit(4)
main_circuit.compose(qaoa_circuit, inplace=True)
beta_val = np.pi * np.random.rand(depth)
gamma_val = 2 * np.pi * np.random.rand(depth)
initial_parameters = list(beta_val) + list(gamma_val)
main_circuit = main_circuit.assign_parameters(initial_parameters)

circuit_from_qasm(qasm2.dumps(main_circuit))

Cirq version

cirq-core==1.5.0.dev20250128015450

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug-reportSomething doesn't seem to work.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions