Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated Classical Registers in QASM Definition when using qc.measure(qr, cr) #302

Open
MattePalte opened this issue Oct 31, 2024 · 0 comments

Comments

@MattePalte
Copy link

Environment

  • pytket version: 1.33.1
  • qiskit version: 1.2.4
  • bqskit version: 1.2.0

Expected behavior
When creating a simple empty circuit with a single final measurement, the exported QASM file should have only one definition for the classical register.

Actual behavior
The exported QASM file has two definitions for the classical register, which leads to an invalid QASM file that cannot be imported by either BQSKit or Qiskit.

Additional information
The reproducibility of this issue is full (100%). The error is raised every time the circuit is exported and imported.

Source code
Here is the minimal example to generate the buggy QASM file:

from bqskit.ext import qiskit_to_bqskit
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

# Section: Circuit
qr = QuantumRegister(2, name='qr')
cr = ClassicalRegister(2, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')

qc.measure(qr, cr)

bqc = qiskit_to_bqskit(qc)
print(bqc)
# Circuit(2)[measurement@(0,), measurement@(1,)]
bqc.save('file.qasm')
# OPENQASM 2.0;
# include "qelib1.inc";
# qreg q[2];
# creg cr[2];
# creg cr[2];
# measure q[0] -> cr[0];
# measure q[1] -> cr[1];

Tracebacks

When trying to import the QASM file with Qiskit:

from qiskit.qasm2 import load
qc = load('file.qasm')
# QASM2ParseError: "file.qasm:5,5: 'cr' is already defined"

When trying to import the QASM file with BQSKit:

from bqskit import Circuit
bqc = Circuit.from_file('file.qasm')
# LangException: Classical register redeclared: cr.

Potential Solution/Insight:

The issue may be due to the exporter generating duplicate instructions for classical registers. This could be caused by a bug in the QASM generation or instruction creation process. Investigating the generation of QASM for classical registers may help identify the root cause of the issue, which may be related to the number of times the register is exported.
Note that when the size of the register is n the number of times the register is exported is n (tested with size up to n=32).
This seems to happen because of the presence of the qc.measure(qr, cr) instruction, so the issue may be related to the handling of this instruction in the QASM generation process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant