Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

QPy roundtripping discards physical qubit information on circuit #10112

Closed
taalexander opened this issue May 15, 2023 · 0 comments · Fixed by #10148
Closed

QPy roundtripping discards physical qubit information on circuit #10112

taalexander opened this issue May 15, 2023 · 0 comments · Fixed by #10148
Assignees
Labels
bug Something isn't working
Milestone

Comments

@taalexander
Copy link
Contributor

taalexander commented May 15, 2023

Environment

  • Qiskit Terra version: main
  • Python version: 3.9
  • Operating system: OSX

What is happening?

When roundtripping a transpiled circuit for a backend, and then dumping to OpenQASM 3 the circuit is no longer on physical qubits when emitted to OpenQASM 3. This impacts the backend when circuit merging is not applied.

How can we reproduce the issue?

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

with open('transpiled.qpy', 'wb') as fd:
    qpy.dump(qc_transpiled, fd)

with open('transpiled.qpy', 'rb') as fd:
    qc_transpiled = qpy.load(fd)[0]

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))

Emits

OPENQASM 3;
include "stdgates.inc";
bit[1] c;
qubit[7] _all_qubits;
let q = _all_qubits[0:6];
x q[0];

What should happen?

Commenting out the QPy roundtripping

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

#with open('transpiled.qpy', 'wb') as fd:
#    qpy.dump(qc_transpiled, fd)

#with open('transpiled.qpy', 'rb') as fd:
#    qc_transpiled = qpy.load(fd)[0]

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))

Gives

OPENQASM 3;
include "stdgates.inc";
bit[1] c;
x $0;

Any suggestions?

This may be fixed by setting the layout on roundtripping

from qiskit import qasm3, transpile, QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import qpy
from qiskit.transpiler import Layout

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(1, 1)
qc.x(0)

qc_transpiled = transpile(qc, backend)

with open('transpiled.qpy', 'wb') as fd:
    qpy.dump(qc_transpiled, fd)

with open('transpiled.qpy', 'rb') as fd:
    qc_transpiled = qpy.load(fd)[0]


qc_transpiled._layout = Layout({qubit: idx for idx, qubit in enumerate(qc_transpiled.qubits)})

print(qc_qasm3 := qasm3.dumps(qc_transpiled, disable_constants=True, experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1))
@taalexander taalexander added the bug Something isn't working label May 15, 2023
@mtreinish mtreinish added this to the 0.24.1 milestone May 15, 2023
@mtreinish mtreinish self-assigned this May 15, 2023
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue May 23, 2023
This commit adds the missing support for QuantumCircuit.layout to the
qpy format. This necessitates bumping the QPY format version to 8 to
accomodate the extra data needed for representing the details of the
layout. The tricky piece with representing the 3 TranspileLayout
attributes is representing the virtual bits in the initial layout
because there is no guarantee that the input circuit's registers are in
the output circuit (typically they are not when transpile() is used).

Fixes Qiskit#10112
blakejohnson pushed a commit to blakejohnson/qiskit-ibm-runtime that referenced this issue May 26, 2023
* Add support for exporting qasm3.

* Address issue in Qiskit/qiskit#10112
@jlapeyre jlapeyre mentioned this issue May 31, 2023
4 tasks
@kdk kdk modified the milestones: 0.24.1, 0.24.2 Jun 1, 2023
github-merge-queue bot pushed a commit that referenced this issue Jul 7, 2023
* Add support for QuantumCircuit.layout to qpy

This commit adds the missing support for QuantumCircuit.layout to the
qpy format. This necessitates bumping the QPY format version to 8 to
accomodate the extra data needed for representing the details of the
layout. The tricky piece with representing the 3 TranspileLayout
attributes is representing the virtual bits in the initial layout
because there is no guarantee that the input circuit's registers are in
the output circuit (typically they are not when transpile() is used).

Fixes #10112

* Fix handling of empty layout

* Expand test coverage

* Fix lint

* Add qpy compat tests

* Fix compat tests

* Add release notes

* Adjust layout creation to be register independent

* Finish docs

* Only check layout in compat tests with circuits

* Fix typos

* Fix doc typo in qiskit/qpy/__init__.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Adjust introduction version for layout qpy compat tests

* Unify qpy compat test version filter style

* Add new line to layout error message

* Simplify serialization logic

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>

* Doc fixes

* Improve test coverage

* Don't reuse bits between initial layout and circuit in qpy compat tests.

* Update qiskit/qpy/__init__.py

* Fix test typo

* Use a register in compat tests for consistent equality

* Update test/python/qpy/test_circuit_load_from_qpy.py

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
mergify bot pushed a commit that referenced this issue Jul 7, 2023
* Add support for QuantumCircuit.layout to qpy

This commit adds the missing support for QuantumCircuit.layout to the
qpy format. This necessitates bumping the QPY format version to 8 to
accomodate the extra data needed for representing the details of the
layout. The tricky piece with representing the 3 TranspileLayout
attributes is representing the virtual bits in the initial layout
because there is no guarantee that the input circuit's registers are in
the output circuit (typically they are not when transpile() is used).

Fixes #10112

* Fix handling of empty layout

* Expand test coverage

* Fix lint

* Add qpy compat tests

* Fix compat tests

* Add release notes

* Adjust layout creation to be register independent

* Finish docs

* Only check layout in compat tests with circuits

* Fix typos

* Fix doc typo in qiskit/qpy/__init__.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Adjust introduction version for layout qpy compat tests

* Unify qpy compat test version filter style

* Add new line to layout error message

* Simplify serialization logic

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>

* Doc fixes

* Improve test coverage

* Don't reuse bits between initial layout and circuit in qpy compat tests.

* Update qiskit/qpy/__init__.py

* Fix test typo

* Use a register in compat tests for consistent equality

* Update test/python/qpy/test_circuit_load_from_qpy.py

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit dbf1230)
github-merge-queue bot pushed a commit that referenced this issue Jul 10, 2023
* Add support for QuantumCircuit.layout to qpy

This commit adds the missing support for QuantumCircuit.layout to the
qpy format. This necessitates bumping the QPY format version to 8 to
accomodate the extra data needed for representing the details of the
layout. The tricky piece with representing the 3 TranspileLayout
attributes is representing the virtual bits in the initial layout
because there is no guarantee that the input circuit's registers are in
the output circuit (typically they are not when transpile() is used).

Fixes #10112

* Fix handling of empty layout

* Expand test coverage

* Fix lint

* Add qpy compat tests

* Fix compat tests

* Add release notes

* Adjust layout creation to be register independent

* Finish docs

* Only check layout in compat tests with circuits

* Fix typos

* Fix doc typo in qiskit/qpy/__init__.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* Adjust introduction version for layout qpy compat tests

* Unify qpy compat test version filter style

* Add new line to layout error message

* Simplify serialization logic

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>

* Doc fixes

* Improve test coverage

* Don't reuse bits between initial layout and circuit in qpy compat tests.

* Update qiskit/qpy/__init__.py

* Fix test typo

* Use a register in compat tests for consistent equality

* Update test/python/qpy/test_circuit_load_from_qpy.py

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit dbf1230)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants