Skip to content

from_qasm_str not importing Fswap correctly #13339

@dberthault

Description

@dberthault

Environment

  • Qiskit version: 1.2.4
  • Python version: 3.12.2
  • Operating system: Linux

What is happening?

When one creates a circuit with qiskit.QuantumCircuit.from_qasm_str an Fswap gate will sometimes be mapped to swap. This seems to only happen if there is a swap before the fswap in the qasm file.

swap q[0],q[1];
fswap q[0],q[1];
fswap q[0],q[1];

gives

CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())

But

fswap q[0],q[1];
swap q[0],q[1];
fswap q[0],q[1];

gives

CircuitInstruction(operation=Instruction(name='fswap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='fswap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())

How can we reproduce the issue?

Run this code:

from qiskit import QuantumCircuit

qasm_str = """OPENQASM 2.0;

gate u3(theta,phi,lambda) q { U(theta,phi,lambda) q; }
gate u2(phi,lambda) q { U(pi/2,phi,lambda) q; }
gate u1(lambda) q { U(0,0,lambda) q; }
gate rx(theta) a { u3(theta,-pi/2,pi/2) a; }
gate ry(theta) a { u3(theta,0,0) a; }
gate rz(phi) a { u1(phi) a; }
gate cx c,t { CX c,t; }

gate swap a,b { cx a,b; cx b,a; cx a,b; }
gate fswap a,b { rz(-pi/2) a; rz(-pi/2) b; rx(pi/2) a; cx a,b; rx(-pi/2) a; ry(-pi/2) b; cx a,b; rx(-pi/2) a; }

qreg q[2];

swap q[0],q[1];
fswap q[0],q[1];
fswap q[0],q[1];"""

qiskit_circuit = QuantumCircuit.from_qasm_str(qasm_str)
for gate in qiskit_circuit:
    print(gate)
qiskit_circuit.draw()

It outputs

CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())

        ┌───────┐┌───────┐
q_0: ─X─┤0      ├┤0      ├
      │ │  Swap ││  Swap │
q_1: ─X─┤1      ├┤1      ├
        └───────┘└───────┘

What should happen?

I should output

CircuitInstruction(operation=Instruction(name='swap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='fswap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())
CircuitInstruction(operation=Instruction(name='fswap', num_qubits=2, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)), clbits=())

        ┌────────┐┌────────┐
q_0: ─X─┤0       ├┤0       ├
      │ │  Fswap ││  Fswap │
q_1: ─X─┤1       ├┤1       ├
        └────────┘└────────┘

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmod: qasm2Relating to OpenQASM 2 import or export

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions