Duplicate declaration error from when converting a Pennylane circuit to QASM language

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments
    I am just converting a Pennylane circuit to QASM language using this line of code: dev._circuit.qasm(formatted=True,filename=filename). When trying to run the same file, I am getting the error listed below.
def eval(self, images, labels):
counter=0
for label,image in zip(labels, images):
            self.circuit(self,self.weights,image)
            filename='C:/Users/gandhi.ha/code/AMP/image_0=' + str(counter)  +'_'+ str(0) +  '.qasm'
            cirs = dev._circuit.qasm(formatted=True,filename=filename)
            np.save('C:/Users/gandhi.ha/code/AMP/label_0=' + str(counter) + '_'+ str(0) +'.npy',label)
            counter+=1 
      return 



from qiskit import execute
from qiskit import Aer 
qasm_list = []
qasm_file =glob.glob('C:/Users/gandhi.ha/code/AMP/*_0.qasm',recursive= True) #C:\Users\gandhi.ha\code\AMP
for file in qasm_file :
    q=QuantumCircuit.from_qasm_file(file)
    qasm_list.append(q)
    Q= QuantumCircuit.depth(q)
IBMQ.save_account('your IBM token', overwrite = True)
IBMQ.load_account()
provider = IBMQ.get_provider(hub ='ibm-q', group = 'open' ,project ='main')
job=execute(qasm_list,provider.get_backend('ibmq_qasm_simulator'),shots=1000) 
result=job.result().get_counts()
probs_list=[]
count_3=0
for i in result: 
    np.save('C:/Users/gandhi.ha/code/PROBS_AMP/data_0='+str(count_3)+'_'+str(0)+'.npy',i)
    count_3+=1

If you want help with diagnosing an error, please put the full error message below:

"Duplicate declaration for gate 'multiplex1_reverse_dg' at line 7, file C:/Users/gandhi.ha/code/AMP\\image_0=0_0.qasm.\nPrevious occurrence at line 4, file C:/Users/gandhi.ha/code/AMP\\image_0=0_0.qasm"```
And, finally, make sure to include the versions of your packages. Specifically, show us the output of `qml.about()`. 

Name: PennyLane
Version: 0.30.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: c:\users\gandhi.ha\appdata\local\programs\python\python310\lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Windows-10-10.0.19045-SP0
Python version:          3.10.5
Numpy version:           1.23.1
Scipy version:           1.9.0
Installed devices:
- default.gaussian (PennyLane-0.30.0)
- default.mixed (PennyLane-0.30.0)
- default.qubit (PennyLane-0.30.0)
- default.qubit.autograd (PennyLane-0.30.0)
- default.qubit.jax (PennyLane-0.30.0)
- default.qubit.tf (PennyLane-0.30.0)
- default.qubit.torch (PennyLane-0.30.0)
- default.qutrit (PennyLane-0.30.0)
- null.qubit (PennyLane-0.30.0)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- qiskit.aer (PennyLane-qiskit-0.28.0)
- qiskit.basicaer (PennyLane-qiskit-0.28.0)
- qiskit.ibmq (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.28.0)

Hey @Harshitta_Gandhi! Welcome to the forum :smiley:

There are files on your machine that you are using that I don’t have. So, I can’t run your code. That said, the error message leads me to believe that there’s something the matter with your .qasm file.

To help you further, I’ll need access to the files that your code needs to run :slight_smile:

Hey @isaacdevlugt, thanks for reaching out to me.

The .qasm file that you’re referring to, looks like this:
OPENQASM 2.0;
include “qelib1.inc”;
gate multiplex1_dg q0 { rz(pi/2) q0; }
gate multiplex1_reverse_dg q0 { rz(pi/2) q0; }
gate multiplex2_dg q0,q1 { multiplex1_reverse_dg q0; cx q1,q0; multiplex1_dg q0; }
gate multiplex1_reverse_reverse_dg q0 { ry(0.049846801) q0; }
gate multiplex1_reverse_dg q0 { ry(0.049846801) q0; }
gate multiplex2_reverse_dg q0,q1 { multiplex1_reverse_dg q0; cx q1,q0; multiplex1_reverse_reverse_dg q0; }
gate multiplex1_dg q0 { rz(-pi/2) q0; }
gate disentangler_dg q0,q1 { multiplex1_dg q1; multiplex2_reverse_dg q0,q1; multiplex2_dg q0,q1; }
gate state_preparation(param0,param1,param2,param3) q0,q1 { disentangler_dg q0,q1; }
gate initialize(param0,param1,param2,param3) q0,q1 { reset q0; reset q1; state_preparation(0.99875791,-0.049826161,0,0) q0,q1; }
qreg q[2];
creg c[2];
initialize(0.99875791,-0.049826161,0,0) q[1],q[0];
rx(1.8898371) q[0];
ry(0.19745321) q[1];
cx q[0],q[1];
measure q[0] → c[0];
measure q[1] → c[1];

It was generated using the pennylane-qiskit plugin to convert an amplitude embedded pennylane circuit to a .qasm file.

I am still getting a duplicate declaration error. Please let me know if there is some other information that I can provide you with for you to regenerate the error.

Hey @Harshitta_Gandhi, thanks for that! I still can’t run your code because I don’t have access to all of the code and some files. However, it seems like this type of issue has been reported and dealt with (?) on qiskit-terra’s side:

Does any of this help?

Upgrading the plugins isn’t helping. I re-generated all the qasm files from the pennylane circuit which now gives me another error.

I am sorry for the inconvenience in replicating the error.

  1. Please store the qasm code in a simple text file with a ‘.qasm’ extension, in a relevant location.
include "qelib1.inc";
gate multiplex1_dg q0 { rz(-pi/2) q0; }
gate multiplex1_reverse_dg q0 { ry(0.04984680120873709) q0; }
gate multiplex1_reverse_reverse_dg q0 { ry(0.04984680120873709) q0; }
gate multiplex2_reverse_dg q0,q1 { multiplex1_reverse_dg q0; cx q1,q0; multiplex1_reverse_reverse_dg q0; }
gate multiplex1_reverse_dg_1740784756400 q0 { rz(pi/2) q0; }
gate multiplex1_dg_1740785057504 q0 { rz(pi/2) q0; }
gate multiplex2_dg q0,q1 { multiplex1_reverse_dg_1740784756400 q0; cx q1,q0; multiplex1_dg_1740785057504 q0; }
gate disentangler_dg q0,q1 { multiplex1_dg q1; multiplex2_reverse_dg q0,q1; multiplex2_dg q0,q1; }
gate state_preparation(param0,param1,param2,param3) q0,q1 { disentangler_dg q0,q1; }
gate initialize(param0,param1,param2,param3) q0,q1 { reset q0; reset q1; state_preparation(0.9987579054229877,-0.049826161352107795,0,0) q0,q1; }
qreg q[2];
creg c[2];
initialize(0.9987579054229877,-0.049826161352107795,0,0) q[1],q[0];
rx(1.889837072393385) q[0];
ry(0.19745320908368108) q[1];
cx q[0],q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];
  1. use the following code to replicate the error.
    pip install -q -U pennylane datasets matplotlib tqdm torchvision torch gupload Qiskit statistics pennylane-qiskit
    python -m pip install qiskit --upgrade
    qasm_file_path = ‘your_qasm_file_path’
    qc = QuantumCircuit.from_qasm_file(qasm_file_path)
    backend = BasicAer.get_backend(‘qasm_simulator’)
    job=execute(qc,backend,shots=1000) # ibmq_qasm_simulator ibm_nairobi
    result=job.result().get_counts()
    probs_list=

  2. The new error that I am facing is:
    QasmError: “Expected an ID, received ‘reset’”

I see that you made a post on this github issue. That was the right call :+1:! I think we can wait and see what the qiskit-terra devs have to say about it.

Yeah, I thought so too
:+1:

1 Like

In the mean time, if you have any other questions please let us know!

1 Like