tket icon indicating copy to clipboard operation
tket copied to clipboard

Pytket QASM Importer Bug: Unsupported ISwap Gate `Gate has an invalid number of parameters`

Open MattePalte opened this issue 1 year ago • 1 comments

Expected behavior

When using Pytket to import a QASM file containing an ISwap gate generated by Qiskit, we expect the import to succeed without any errors. The Pytket importer should be able to correctly parse the ISwap gate and its parameters.

Actual behavior

The Pytket importer crashes when trying to import the QASM file, throwing a "Gate has an invalid number of parameters" error. This error occurs because the Pytket importer does not support the ISwap gate with two parameters.

Additional information

This issue is 100% reproducible. We have verified that the Qiskit-generated QASM file is correct and can be imported by other tools. The issue seems to be specific to the Pytket importer.

Source code

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.qasm import qasm_data
from pytket.qasm import circuit_from_qasm_str

# Create a Qiskit circuit with an ISwap gate
qr = QuantumRegister(3, 'qr')
cr = ClassicalRegister(3, 'cr')
qc = QuantumCircuit(qr, cr, name='qc')
qc.iswap(qr[1], qr[2])

# Export the Qiskit circuit to a QASM file
qasm_content = qc.qasm()

# Try to import the QASM file using Pytket
try:
    circuit = circuit_from_qasm_str(qasm_content)
    print(f'Circuit (Pytket) imported correctly: ')
except Exception as e:
    print(e)  # Gate has an invalid number of parameters

print(qasm_content)
# Output:
# OPENQASM 2.0;
# include "qelib1.inc";
# gate iswap q0,q1 { s q0; s q1; h q0; cx q0,q1; cx q1,q0; h q1; }
# qreg qr[3];
# creg cr[3];
# iswap qr[1],qr[2];

Tracebacks

Gate has an invalid number of parameters

System

import qiskit
import pytket
print(f"Qiskit version: {qiskit.__version__}")
print(f"Pytket version: {pytket.__version__}")

Output:

Qiskit version: 1.2.0
Pytket version: 1.31.1

MattePalte avatar Oct 04 '24 14:10 MattePalte

I think this bug was introduced by #612 . The idea there was that pytket would add necessary gate definitions for conversion of certain known pytket gates to QASM, including OpType.ISWAP (which in TKET is a one-parameter gate). Somehow when converting in the other direction when we encounter such a gate (in this case iswap) it is assumed that it corresponds to this definition, so pytket tries to convert it to its own OpType.ISWAP and fails.

cqc-alec avatar Oct 07 '24 12:10 cqc-alec