Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.83k stars 2.29k forks source link

Fix `replace_block_with_op` on operations with wrong number of qubits #12637

Closed Cryoris closed 2 days ago

Cryoris commented 1 week ago

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
qiskit-bot commented 1 week ago

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

coveralls commented 1 week ago

Pull Request Test Coverage Report for Build 9615440134

Details


Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 4 93.13%
<!-- Total: 4 -->
Totals Coverage Status
Change from base Build 9614505164: 0.04%
Covered Lines: 63528
Relevant Lines: 70776

💛 - Coveralls
ElePT commented 1 week ago

The times of the good old multi-X gate are over.

coveralls commented 1 week ago

Pull Request Test Coverage Report for Build 9647348633

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details


Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/generalized_gates/permutation.py 1 92.86%
qiskit/circuit/library/standard_gates/xx_plus_yy.py 2 95.74%
qiskit/circuit/library/standard_gates/xx_minus_yy.py 2 95.74%
crates/qasm2/src/lex.rs 6 92.37%
crates/qasm2/src/parse.rs 6 97.15%
crates/circuit/src/operations.rs 79 73.24%
<!-- Total: 96 -->
Totals Coverage Status
Change from base Build 9614505164: 0.05%
Covered Lines: 63661
Relevant Lines: 70917

💛 - Coveralls
Cryoris commented 2 days ago

@Mergifyio backport stable/0.46 stable/1.1

mergify[bot] commented 2 days ago

backport stable/0.46 stable/1.1

✅ Backports have been created

* [#12699 Fix `replace_block_with_op` on operations with wrong number of qubits (backport #12637)](https://github.com/Qiskit/qiskit/pull/12699) has been created for branch `stable/0.46` * [#12700 Fix `replace_block_with_op` on operations with wrong number of qubits (backport #12637)](https://github.com/Qiskit/qiskit/pull/12700) has been created for branch `stable/1.1`