Skip to content

Strange Behaviour of sx gate when defined inline in QASM #12167

@DanBlackwell

Description

@DanBlackwell

Environment

  • Qiskit version: 1.0.2
  • Python version: Python 3.10.12
  • Operating system: Ubuntu 22.04

What is happening?

Applying the sx gate seems to give a different result if I copy and paste the x and sx gate definitions out from stdgates.inc, versus if I use include stdgates.inc.

How can we reproduce the issue?

from qiskit import transpile
from qiskit.qasm3 import loads
from qiskit.quantum_info import Statevector
from qiskit_aer import Aer

qc = loads('''
OPENQASM 3.0;
gate x a { U(pi, 0, pi) a; }
gate sx a { pow(1/2) @ x a; }
qubit[1] r1;
sx r1[0];
''')
qc = transpile(qc, Aer.get_backend('qasm_simulator'))
state_vec = Statevector.from_instruction(qc)
print(state_vec)
print(state_vec.probabilities())

Output:

Statevector([1.+0.j, 0.+0.j],
            dims=(2,))
[1. 0.]

What should happen?

Using the include instead:

from qiskit import transpile
from qiskit.qasm3 import loads
from qiskit.quantum_info import Statevector
from qiskit_aer import Aer

qc = loads('''
OPENQASM 3.0;
include "stdgates.inc";
qubit[1] r1;
sx r1[0];
''')
qc = transpile(qc, Aer.get_backend('qasm_simulator'))
state_vec = Statevector.from_instruction(qc)
print(state_vec)
print(state_vec.probabilities())

Gets the correct probabilities:

Statevector([0.5+0.5j, 0.5-0.5j],
            dims=(2,))
[0.5 0.5]

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmod: qasm3Related to OpenQASM 3 import or export

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions