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

QASM Exporter Crash due to Unusual Numerical Register Name #559

Closed
MattePalte opened this issue Jan 2, 2025 · 1 comment
Closed

QASM Exporter Crash due to Unusual Numerical Register Name #559

MattePalte opened this issue Jan 2, 2025 · 1 comment
Assignees

Comments

@MattePalte
Copy link

System version

# Install Qiskit version 1.2.0
RUN pip install --no-cache-dir qiskit==1.2.1
RUN pip install qiskit-aer==0.15.1 qiskit-ibm-runtime==0.29.1
RUN pip install pytket==1.32.0 pytket-qiskit==0.56.0

Expected behavior

I expect the code to successfully export a Qiskit circuit to a Pytket QASM file without any errors, e.g. by renaming the registers to conform to the QASM specification. Because this would be more robust and in line with a user expects from other quantum computing platforms such as Qiskit and BQSKit.

Actual behavior

The code raises a QASMUnsupportedError when trying to export the Qiskit circuit to a Pytket QASM file. The error occurs because the platform tries to export a QASM with a register name '4' which is invalid according to the QASM specification.

Additional information

This issue occurs every time the code is run with the same register names. The error does not occur when we either use another platform (e.g. Qiskit or BQSKit) or rename the registers to conform to the QASM specification. Note that the other platforms rename the registers automatically to conform to the QASM specification.

Source code

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import qasm2
from pytket.extensions.qiskit import qiskit_to_tk
from pytket.qasm import circuit_to_qasm_str
from pathlib import Path

# Create quantum and classical registers with valid names
qr = QuantumRegister(3, '4')
cr = ClassicalRegister(3, 'c4')
circuit = QuantumCircuit(qr, cr)
print(circuit.draw(output='text'))

# Export Qiskit circuit to QASM file using qasm2
qasm_str_qiskit = qasm2.dumps(circuit)
print("Qiskit-generated QASM:")
print(qasm_str_qiskit)

# Export Qiskit circuit to Pytket QASM file
tket_circ = qiskit_to_tk(circuit)
qasm_str_tket = circuit_to_qasm_str(tket_circ, header='qelib1', maxwidth=200)
file_path_pytket = Path('simple_pytket.qasm')
with open(file_path_pytket, 'w') as f:
    f.write(qasm_str_tket)
print(f'Saved the Pytket circuit to {file_path_pytket}')
# Expected output: the QASM file is successfully generated and saved

Tracebacks

---------------------------------------------------------------------------
QASMUnsupportedError                      Traceback (most recent call last)
Cell In[5], line 21
    19 # Export Qiskit circuit to Pytket QASM file
    20 tket_circ = qiskit_to_tk(circuit)
---> 21 qasm_str_tket = circuit_to_qasm_str(tket_circ, header='qelib1', maxwidth=200)
    22 file_path_pytket = Path('simple_pytket.qasm')
    23 with open(file_path_pytket, 'w') as f:

File /python3.10/site-packages/pytket/qasm/qasm.py:1115, in circuit_to_qasm_str(circ, header, include_gate_defs, maxwidth)
  1100 """Convert a Circuit to QASM and return the string.
  1101
  1102 Classical bits in the pytket circuit must be singly-indexed.
  (...)
  1111 :return: qasm string
  1112 """
  1114 check_can_convert_circuit(circ, header, maxwidth)
-> 1115 qasm_writer = QasmWriter(
  1116     circ.qubits, circ.bits, header, include_gate_defs, maxwidth
  1117 )
  1118 circ1 = circ.copy()
  1119 DecomposeBoxes().apply(circ1)

File /python3.10/site-packages/pytket/qasm/qasm.py:1380, in QasmWriter.__init__(self, qubits, bits, header, include_gate_defs, maxwidth)
  1378 for reg in self.qregs.values():
  1379     if regname_regex.match(reg.name) is None:
-> 1380         raise QASMUnsupportedError(
  1381             f"Invalid register name '{reg.name}'. QASM register names must "
  1382             "begin with a lowercase letter and may only contain lowercase "
  1383             "and uppercase letters, numbers, and underscores. "
  1384             "Try renaming the register with `rename_units` first."
  1385         )
  1386 for bit_reg in self.cregs.values():
  1387     if regname_regex.match(bit_reg.name) is None:

QASMUnsupportedError: Invalid register name '4'. QASM register names must begin with a lowercase letter and may only contain lowercase and uppercase letters, numbers, and underscores. Try renaming the register with `rename_units` first.
@jcqc jcqc assigned cqc-alec and unassigned cqc-alec Jan 6, 2025
@cqc-alec
Copy link
Collaborator

cqc-alec commented Jan 7, 2025

Thanks for the report. I think raising an error in this case is OK: the user is free to rename the registers (as suggested in the error message); and silently renaming them could cause more confusion than it saves.

@cqc-alec cqc-alec closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
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

2 participants