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

Larger barrier than expected following a quantum volume circuit #8910

Open
ihincks opened this issue Oct 14, 2022 · 4 comments
Open

Larger barrier than expected following a quantum volume circuit #8910

ihincks opened this issue Oct 14, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@ihincks
Copy link
Contributor

ihincks commented Oct 14, 2022

Environment

  • Qiskit Terra version: 0.22.0

What is happening?

Construction a quantum volume circuit, followed by a measurement of active qubits, followed by a transpilation to a backend, produces a barrier across all qubits in the device including the ancilla.

How can we reproduce the issue?

import qiskit
from qiskit.circuit.library import QuantumVolume

qiskit.IBMQ.load_account()
provider = qiskit.IBMQ.get_provider()
backend = provider.get_backend('ibm_nairobi')

c = QuantumVolume(3,3)
c.measure_all()

qiskit.transpile(c, backend, initial_layout=[0,1,2]).draw()

image

What should happen?

This behaviour is stochastic depending on the circuit generated, but I expected transpiler to always keep the barrier on only the active physical qubits. I believe this issue is the root of qiskit-community/qiskit-experiments#845, though that issue could also be fixed in other ways.

Any suggestions?

No response

@ihincks ihincks added the bug Something isn't working label Oct 14, 2022
@mtreinish
Copy link
Member

I believe that is intentional behavior, I ran a quick test of your code with a callback set:

from qiskit.converters import dag_to_circuit

def callback(**kwargs):
    print(kwargs["pass_"])
    print(dag_to_circuit(kwargs["dag"]))

transpile(qc, backend, initial_layout=[0,1,2], callback=callback)

and the barrier is being added by the BarrierBeforeFinalMeasurements pass which is intentionally adding a barrier before all final measurements in a circuit: https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/transpiler/passes/utils/barrier_before_final_measurements.py

The idea behind it is to prevent swap mapping from unintentionally inserting a swap after a measurement by inserting a barrier on all qubits. But that pass predates my involvement on the project, maybe @kdk can comment on this more as it looks like he made this pass insert a device wide barrier in #4054

@ihincks
Copy link
Contributor Author

ihincks commented Oct 14, 2022

Thanks, and thanks for the debug callback trick. Do you know what makes BarrierBeforeFinalMeasurements get invoked for the QV circuit, but not for a circuit such as this?

import qiskit
from qiskit.converters import dag_to_circuit

c = qiskit.QuantumCircuit(3)
c.x(0)
c.cnot(0,1)
c.cnot(1,2)

cc = qiskit.QuantumCircuit(*c.qregs)
cc.compose(c.to_instruction(), qubits=cc.qubits, inplace=True)
cc.measure_active()

def callback(**kwargs):
    print(kwargs["pass_"])
    print(dag_to_circuit(kwargs["dag"]))

qiskit.transpile(cc, backend=backend, initial_layout=[0,1,2], callback=callback)

(where I've wrapped a circuit into an instruction to make it look as much like a QV circuit as possible)

@mtreinish
Copy link
Member

My guess is that there is no routing (the transpiler stage that adds swaps to make up for a lack of connectivity on the device to run the circuit) needed. The cnot circuit just needs a 3 nodes that have a path between them which the coupling map will have, while a quantum volume circuit needs routing to run because the device has insufficient connectivity. The barrier before measurement pass only runs when there is routing is run, because it adds the barrier as a guard against the pass adding a swap incorrectly (according to the comments).

@ihincks
Copy link
Contributor Author

ihincks commented Oct 14, 2022

It's looking more and more like it should be qiskit_experiment.ParallelExperiment's job to trim back over-zealous barriers of transpiled circuits. Or QuantumVolumeExperiment._transpiled_circuits() for a more local fix.

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

No branches or pull requests

2 participants