Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cannot convert a teleportation circuit to QASM3 #12013

Open
nonhermitian opened this issue Mar 15, 2024 · 1 comment
Open

Cannot convert a teleportation circuit to QASM3 #12013

nonhermitian opened this issue Mar 15, 2024 · 1 comment
Labels
bug Something isn't working mod: qasm3 Related to OpenQASM 3 import or export

Comments

@nonhermitian
Copy link
Contributor

nonhermitian commented Mar 15, 2024

Environment

  • Qiskit version: 1.1.0.dev0+f48d819
  • Python version: 3.11
  • Operating system: OSX

What is happening?

I should be able to use qiskit.qasm3.dump or qiskit.qasm3.dumps on a dynamic circuit. However, for a teleportation circuit it throws an error

from qiskit import *
from qiskit.quantum_info import random_unitary
# Create a random state for qubit C
random_1q_gate = random_unitary(2).to_instruction()

# Create a register for each qubit of interest
qrA = QuantumRegister(1, 'qA')
qrB = QuantumRegister(1, 'qB')
qrC = QuantumRegister(1, 'qC')
anc_top = QuantumRegister(1, 'ancilla_top')
anc_bottom = QuantumRegister(1, 'ancilla_bottom')

# Create a single classical register
cr = ClassicalRegister(2, 'c')
cr_ancilla = ClassicalRegister(1, 'ancilla_bits')
# Create circuit
qc = QuantumCircuit(anc_top, qrC, qrA, qrB, anc_bottom, cr, cr_ancilla)
qc.reset(range(5))
# Start in the |1> state
qc.x(qrC)
# Make C qubit in random quantum state
qc.compose(random_1q_gate, qrC, inplace=True)
# Form Bell pair between A and B
qc.h(qrA)
qc.cx(qrA, qrB)
qc.barrier()

# Bell measurement
qc.cx(qrC, qrA)
qc.h(qrC)
qc.measure([qrC[0], qrA[0]], [cr[0], cr[1]])

qc.barrier()
# Corrective Pauli gates
with qc.switch(cr) as case:
    # If returned bit-string is 00 = 0
    with case(0):
        pass
    # If returned bit-string is 01 = 1
    with case(1):
        qc.z(qrB)
    # If returned bit-string is 10 = 2
    with case(2):
        qc.x(qrB)
# If returned bit-string is 11 = 3
    with case(3):
        qc.z(qrB)
        qc.x(qrB)

qc.barrier()
# Apply inverse of unitary that generated state C
qc.compose(random_1q_gate.inverse(), qrB, inplace=True)
# We should be back in the ground state
qc.cx(qrB, anc_bottom)
qc.measure(anc_bottom, cr_ancilla[0])
from qiskit import qasm3
qasm3.dumps(qc)

gives

File [~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/qasm3/exporter.py:195](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/qasm3/exporter.py:195), in Exporter.dump(self, circuit, stream)
    [185](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/qasm3/exporter.py:185) """Convert the circuit to OpenQASM 3, dumping the result to a file or text stream."""
    [186](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/qasm3/exporter.py:186) builder = QASM3Builder(
    [187](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/qasm3/exporter.py:187)     circuit,
...
--> [176](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/circuit/tools/pi_check.py:176) complex_inpt = complex(inpt)
    [177](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/circuit/tools/pi_check.py:177) real, imag = map(normalize, [complex_inpt.real, complex_inpt.imag])
    [179](https://file+.vscode-resource.vscode-cdn.net/Users/paul/Desktop/~/mambaforge/envs/qiskit-one/lib/python3.12/site-packages/qiskit/circuit/tools/pi_check.py:179) jstr = "\\jmath" if output == "latex" else "j"

TypeError: only length-1 arrays can be converted to Python scalars

How can we reproduce the issue?

Try the above

What should happen?

I should be able to dump a dynamic circuit to QASM3

Any suggestions?

No response

@nonhermitian nonhermitian added the bug Something isn't working label Mar 15, 2024
@1ucian0 1ucian0 added the mod: qasm3 Related to OpenQASM 3 import or export label Mar 18, 2024
@jakelishman
Copy link
Member

I think the problem here isn't actually the dynamic circuits, it's the UnitaryGate that appears during random_unitary(2).to_instruction():

from qiskit import QuantumCircuit, qasm3
from qiskit.quantum_info import random_unitary

qc = QuantumCircuit(1)
qc.append(random_unitary(2).to_instruction(), qc.qubits)
qasm3.dumps(qc)

shows the same trace.

Obviously still a problem, but I suspect it's more likely to do with our dodgy handling of Instruction.params rather than the dynamic-circuit bits, since UnitaryGate has a Numpy array in its params.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working mod: qasm3 Related to OpenQASM 3 import or export
Projects
None yet
Development

No branches or pull requests

3 participants