-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
bugSomething isn't workingSomething isn't workingmod: qasm2Relating to OpenQASM 2 import or exportRelating to OpenQASM 2 import or export
Milestone
Description
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
Labels
bugSomething isn't workingSomething isn't workingmod: qasm2Relating to OpenQASM 2 import or exportRelating to OpenQASM 2 import or export