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

Fix replace_block_with_op on operations with wrong number of qubits (backport #12637) #12699

Merged
merged 1 commit into from
Jul 2, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jul 1, 2024

Summary

Previously, DAGCircuit.replace_block_with_op allowed to place an
n-qubit operation onto a block of m qubits, leaving the DAG in an
invalid state. This behavior has been fixed, and the attempt will raise
a DAGCircuitError.

Details and comments

For example, this is currently possible:

from qiskit import transpile
from qiskit.circuit import QuantumCircuit
from qiskit.converters import circuit_to_dag, dag_to_circuit
from qiskit.circuit.library import XGate

# i am fine
qc = QuantumCircuit(2)
qc.x(range(2))

# a bit of dag mutilation
dag = circuit_to_dag(qc)
to_replace = list(dag.op_nodes())
new_node = XGate()
idx_map = {node.qargs[0]: i for i, node in enumerate(to_replace)}
dag.replace_block_with_op(to_replace, new_node, idx_map)

# the result
midlifecrisis = dag_to_circuit(dag)
print(midlifecrisis)

# naturally breaks
tqc = transpile(midlifecrisis, basis_gates=["u", "cx"])
print(tqc)

which prints

     ┌────┐
q_0: ┤0   ├
     │  X │
q_1: ┤1   ├
     └────┘
# and then breaks upon transpilation

This is an automatic backport of pull request #12637 done by [Mergify](https://mergify.com).

…#12637)

* fix illegal op insertion

* rm dangling print

* fix PauliEvolution

* Update qiskit/dagcircuit/dagcircuit.py

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

---------

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
(cherry picked from commit 67fd35a)
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@github-actions github-actions bot added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Jul 1, 2024
@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9747108484

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 2 files are covered.
  • 15 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.01%) to 87.202%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.88%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 9687482615: -0.01%
Covered Lines: 74520
Relevant Lines: 85457

💛 - Coveralls

@ElePT ElePT added this pull request to the merge queue Jul 2, 2024
Merged via the queue into stable/0.46 with commit 39e2dac Jul 2, 2024
12 checks passed
@mergify mergify bot deleted the mergify/bp/stable/0.46/pr-12637 branch July 2, 2024 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants