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

Wrong decomposition rule when decomposing into a single-qubit instruction #8591

Closed
nathanieltornow opened this issue Aug 21, 2022 · 0 comments · Fixed by #8614
Closed

Wrong decomposition rule when decomposing into a single-qubit instruction #8591

nathanieltornow opened this issue Aug 21, 2022 · 0 comments · Fixed by #8614
Labels
bug Something isn't working

Comments

@nathanieltornow
Copy link

nathanieltornow commented Aug 21, 2022

Environment

  • Qiskit Terra version: 0.21.1
  • Python version: 3.9.13
  • Operating system: macOS Monterey

What is happening?

I use placeholder instructions with one or more qubits and clbits to create circuits in different configurations.
The placeholders are created with the maximum amount of qubits and clbits needed, which might be more than needed for some specific configurations.

However, if I put one single-qubit instruction into a placeholder with 1 qubit and C clbits, circuit.decompose() fails with

qiskit.dagcircuit.exceptions.DAGCircuitError: 
'Cannot replace node of width (1 qubits, C clbits) with instruction of mismatched width (1 qubits, 0 clbits).'

How can we reproduce the issue?

from qiskit import QuantumCircuit

placeholder = QuantumCircuit(1, 1)
placeholder.h(0)
# placeholder.x(0) # uncomment this line to make the example work

pld_instr = placeholder.to_instruction()

circuit = QuantumCircuit(3, 3)
circuit.h(0)
circuit.append(placeholder, [0], [0])
circuit.x(1)

print(circuit)
print(circuit.decompose()) # FAILS

What should happen?

The decomposition pass should not fail, and behave the same as if multiple instructions would have been put into the placeholder. (See example when uncomment line 5)

Any suggestions?

As far as I see it, the problem arises because of this if-statement in the decomposition pass:

rule = node.op.definition.data
if len(rule) == 1 and len(node.qargs) == len(rule[0].qubits) == 1:
    if node.op.definition.global_phase:
        dag.global_phase += node.op.definition.global_phase
    dag.substitute_node(node, rule[0].operation, inplace=True)

This maybe seems to be a optimization (?) that can quickly handle a single-qubit instruction. However, I think it does not handle the case when the instruction does not have the same amount of clbits as the placeholder.

The simplest fix would be to omit the if statement, and execute the else-statement

else:
    decomposition = circuit_to_dag(node.op.definition)
    dag.substitute_node_with_dag(node, decomposition)

in any case.

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.

1 participant