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

Add support for BackendV2 and Target to transpiler #7113

Closed
mtreinish opened this issue Oct 8, 2021 · 5 comments
Closed

Add support for BackendV2 and Target to transpiler #7113

mtreinish opened this issue Oct 8, 2021 · 5 comments
Assignees
Labels
ISA ISA-related issues type: enhancement It's working, but needs polishing

Comments

@mtreinish
Copy link
Member

What is the expected enhancement?

With the BackendV2/Target interface being added in #5885 we have more data available to the transpiler to inform the passes on the characteristics and constraints of a backend. However as of right now none of the passes in the transpiler know how to take advantage of this extra information. We need to update the transpile()/PassManagerConfig/preset pass manager constructors to deal with passing a Target object around and then update some passes to leverage the extra information.

Primarily for 0.19.0 we probably will need to update the BasisTranslator to respect the qargs of an instruction, CheckGateDirection/GateDirection to work with Target instead of a coupling map, and optionally UnitarySynthesis to get noise parameters from a Target. While pretty much every other pass will need to be updated too those can happen over time the first 2 should be enough so transpile() will respect a heterogeneous gate set which is our goal for 0.19.0.

@mtreinish mtreinish added type: enhancement It's working, but needs polishing priority: high labels Oct 8, 2021
@mtreinish mtreinish added this to the 0.19 milestone Oct 8, 2021
@mtreinish mtreinish self-assigned this Oct 8, 2021
@mtreinish mtreinish added the ISA ISA-related issues label Oct 8, 2021
@mtreinish mtreinish modified the milestones: 0.19, 0.20 Nov 30, 2021
@mtreinish
Copy link
Member Author

I'm changing the target here to 0.20.0, because while the first piece of adding the target to the PassManagerConfig is done in #7227 and the basis translator will have initial support for working with a Target we still need to update all the other passes and start us down the path to making the target used everywhere which will be a work item for 0.20.0

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Jan 20, 2022
This commit makes the GatesInBasis transpiler pass target aware. It adds
a new kwarg, target, which optionally takes a Target object. If it's
specified the basis_gates required field will be ignored and the pass
will check that the gates and their qargs are valid in the target.

Part of Qiskit#7113
@mtreinish
Copy link
Member Author

mtreinish commented Mar 4, 2022

So for this issue I think we should concentrate just on passes that are either basis aware or noise aware. These are places where the target provides a more rich set of information and to do a thorough job compiling for a target based backend we need the full set of information instead of just the global backwards compatibility shims. For example, things like layout and routing passes don't really gain much by using the target as they operate on a global coupling graph and rely on later passes to adjust things for basis gates, etc (the exception being noise aware versions of these passes).

Once we've migrated the passes where there is a functional need I feel like we can close this issue. After this we should look at expanding the functionality in the transpiler based on the extra information in the target. The big use case is updating things to leverage over complete basis gates (i.e. have the transpiler pick the best gate if there are multiple choices available) or to work with fixed angle tuned variants of arbitrary rotation gates.

I've made a list of these passes here:

(I only grepped for keywords and I might have missed some, so feel free to expand the list)

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Mar 4, 2022
This commit updates the VF2Layout pass to be target aware. It adds a new
kwarg for specifying a target and if it's specified that target object
is used to define the parameters for finding the isomorphic mapping used
for the layout. However, with this commit the extra information contained in
the target isn't really leveraged yet and just the global coupling graph
and measurement error rates are pulled from the target just as in the
BackendV1 case. This commit is mostly to facilitate future expansion where
we will improve the layout scoring heuristic used and having the full set
of data available in the target will be useful.

Part of Qiskit#7113
@HuangJunye
Copy link
Collaborator

I can help you with some of these transpiler passes

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Mar 14, 2022
This commit updates the dense layout pass target aware so that at
its instantiation a Target object can be specified to define the target
device for compilation. When specified this target will be used for
all device aware operations in the pass. When a target is specified the
error matrix used is the average of all 1q and 2q operations while
before the matrix was composed of just cx and readout error rates.

Part of Qiskit#7113
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Mar 14, 2022
This commit updates the consolidate blocks pass target aware so that at
its instantiation a Target object can be specified to define the target
device for compilation. When specified this target will be used for
all device aware operations in the pass. When a target is specified the
consolidate blocks pass will only consolidate blocks with instructions
outside the target (both operation and qubits).

Part of Qiskit#7113
mergify bot pushed a commit that referenced this issue Mar 17, 2022
* Make ConsolidateBlocks transpiler pass target aware

This commit updates the consolidate blocks pass target aware so that at
its instantiation a Target object can be specified to define the target
device for compilation. When specified this target will be used for
all device aware operations in the pass. When a target is specified the
consolidate blocks pass will only consolidate blocks with instructions
outside the target (both operation and qubits).

Part of #7113

* Fix typo

* Add release note

* Add tests for target instruction_supported() method

* Add Target tests for consolidate block

* Apply suggestions from code review

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

* Check number of arguments in ideal operation case

This commit adds a check in the ideal operation case that the number of
qargs is equal to the number of qubits the operation object supports.
This way if you call instruction_supported() with a qargs parameter that
has the wrong number of arguments even an ideally implemented gate will
return false. For example, target.instruction_supported(cx, (0, 1, 2))
will return False since a CXGate object only supports 2 qubits.

* Cast qargs to tuple to prevent issues with qarg list lookups

* Deduplicate qubit index map creation

Co-authored-by: Jake Lishman <jake@binhbar.com>
@mtreinish
Copy link
Member Author

I've crossed CommutativeCancellation off the list because I'm not sure there is anything extra a target can add to the pass. It is used solely to know which z rotation gate to use in the cancellation if there aren't any in the circuit: https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/transpiler/passes/optimization/commutative_cancellation.py#L72-L80 The pass isn't really basis aware per say because by default if there is an rz, p, or u1 in the circuit the pass will use one of those (whichever is first in the circuit) regardless of the basis. The underlying assumption here was probably that this pass was written assuming the circuit was already basis translated prior to execution so that looking at the circuit's gates kind of proxied checking the basis set. The pass could (and arguably should) be rewritten to not take a basis at all and just always use one of those z rotation gates and require that basis translation be run after it. We already run the basis translator in all preset pass managers because other optimization passes don't always emit gates in the target basis.

jakelishman pushed a commit that referenced this issue Mar 19, 2022
* Make DenseLayout transpiler pass target aware

This commit updates the dense layout pass target aware so that at
its instantiation a Target object can be specified to define the target
device for compilation. When specified this target will be used for
all device aware operations in the pass. When a target is specified the
error matrix used is the average of all 1q and 2q operations while
before the matrix was composed of just cx and readout error rates.

Part of #7113

* Adjust error heuristic for target case

This commit adjusts the error heuristic used in dense layout. It now
uses the max() error rate (so worst fidelity) operation on qubits instead
of the average fidelity which was used in the previous commits. It also
removes the weighting in the code based on the number of 1q ops and 2q
ops. Previously the error rate for a layout was calculated as:

    ((sum(1q error for qubits in layout) / num_dag_qubits) * num_1q_ops - avg_1q_err) + ((sum(2q error for edges in layout subgraph) / num_dag_qubits) * num_2q_ops)

however since we're considering all 2q and 1q ops now knowing exactly
what's going to end up in the circuit is not exact so the weighing based
on the number of ops is removed. So when a target is set the value of
`num_1q_ops` and `num_2q_ops` is hard coded to 1.

* Add comment explaining why we use max error rate

* Make coupling_map kwarg optional

* Expand DenseLayout tests

* Apply suggestions from code review

Co-authored-by: Kevin Hartman <kevin@hart.mn>

Co-authored-by: Kevin Hartman <kevin@hart.mn>
@kdk kdk modified the milestones: 0.20, 0.21 Mar 29, 2022
mergify bot added a commit that referenced this issue Mar 30, 2022
* Make GatesInBasis transpiler pass Target aware

This commit makes the GatesInBasis transpiler pass target aware. It adds
a new kwarg, target, which optionally takes a Target object. If it's
specified the basis_gates required field will be ignored and the pass
will check that the gates and their qargs are valid in the target.

Part of #7113

* Update qiskit/transpiler/passes/utils/gates_basis.py

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

* No longer make basis_gates a required argument

Now that we can specify a target or basis_gates it's probably best that
we don't require basis_gates is always set, especially since a target
will always take precedence over the basis_gates list. This commit makes
basis_gates optional, but explicitly raises an error if neither
basis_gates or target is set on the constructor.

* Switch to op_nodes() for internal loop

This commit tweaks the internal loop to be over dag.op_nodes() instead
of dag.topological_op_nodes() which was used in earlier iterations. In
local benchmarking dag.op_nodes() is on average 2x faster than
dag.topological_op_nodes().

* Improve performance of basis_gates path

This commit improves the performance of the basis_gates code path by
combining the basis_gates set with the built-in directives and doing
only one set lookup per iteration instead of two.

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

* Fix support of ideal operations in target

This commit fixes the handling of ideal operations in a target. If an
operation is listed as ideal (where it applies on all qubits and doesn't
have any error or duration information) in a target then the instruction
properties inner dict is set to `{None: None}` to indicate this. For the
gates in basis pass this just means it needs to check if the qubits for
a particular instruction are None, and if it is we can treat it as a
match.

* Use target instruction_supported() method

In #7778 a new target method, instruction_supported(), was added to
simplify checking if an instruction (operation + qubits) is supported on
a target object. This commit switches the GatesInBasis pass to use this
internally instead of manually querying the target.

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
mergify bot added a commit that referenced this issue Mar 31, 2022
* Make VF2Layout pass Target aware

This commit updates the VF2Layout pass to be target aware. It adds a new
kwarg for specifying a target and if it's specified that target object
is used to define the parameters for finding the isomorphic mapping used
for the layout. However, with this commit the extra information contained in
the target isn't really leveraged yet and just the global coupling graph
and measurement error rates are pulled from the target just as in the
BackendV1 case. This commit is mostly to facilitate future expansion where
we will improve the layout scoring heuristic used and having the full set
of data available in the target will be useful.

Part of #7113

* Don't raise in __init__ with no coupling map

* Add release note

* Apply suggestions from code review

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

* Fix test to make it clear it's using target graph and not the coupling map

* Handle edge case where target doesn't have measurement defined

This commit fixes an edge case in the heurstic scoring where if a target
is present but doesn't have measurement defined we always return a score
of 0. In the case measure ment doesn't have measurement defined this
will fall back to looking at the degree of the qubit instead of trying
to use the readout error rate.

* Update qiskit/transpiler/passes/layout/vf2_layout.py

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

* Simplify heuristic scoring logic

Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

* Fix lint

Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Apr 28, 2022
This commit adds two new constructor arguments to the Unroll3qOrMore
transpiler pass, target and basis_gates, which are used to specify a
backend target and basis gate list respectively. If these are specified
the pass will not decompose any multiqubit operations present in the
circuit if they are in the target or basis_gates list.

Fixes Qiskit#5518
Related to Qiskit#7812
Part of Qiskit#7113
mergify bot added a commit that referenced this issue May 4, 2022
* Support target and basis gates in Unroll3qOrMore transpiler pass

This commit adds two new constructor arguments to the Unroll3qOrMore
transpiler pass, target and basis_gates, which are used to specify a
backend target and basis gate list respectively. If these are specified
the pass will not decompose any multiqubit operations present in the
circuit if they are in the target or basis_gates list.

Fixes #5518
Related to #7812
Part of #7113

* Update releasenotes/notes/unroll3q-target-bf57cc4365808862.yaml

Co-authored-by: Naoki Kanazawa <nkanazawa1989@gmail.com>

* Fix lint

* Fix release-note typo

Co-authored-by: Naoki Kanazawa <nkanazawa1989@gmail.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@kdk kdk modified the milestones: 0.21, 0.22 Jun 14, 2022
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Sep 23, 2022
This commit makes the UnrollCustomDefinitions transpiler pass target
aware. A new kwarg, target is added to the pass constructor and if
specified the Target object passed in is used to check if the target
backend supports the gates in the circuit instead of the basis_gates
list.

Part of Qiskit#7113
@mtreinish mtreinish removed this from the 0.22 milestone Sep 26, 2022
mergify bot added a commit that referenced this issue Oct 20, 2022
* Make UnrollCustomDefinitions pass target aware

This commit makes the UnrollCustomDefinitions transpiler pass target
aware. A new kwarg, target is added to the pass constructor and if
specified the Target object passed in is used to check if the target
backend supports the gates in the circuit instead of the basis_gates
list.

Part of #7113

* Apply suggested doc fixes from code review

Co-authored-by: Kevin Hartman <kevin@hart.mn>

* Simplify custom gate detection logic in pass

This commit simplifies and deduplicates the logic for the pass around
detecting whether a particular instruciton is supported.

Co-authored-by: Kevin Hartman <kevin@hart.mn>

Co-authored-by: Kevin Hartman <kevin@hart.mn>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
@mtreinish
Copy link
Member Author

With #9263 recently merging this is finally complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ISA ISA-related issues type: enhancement It's working, but needs polishing
Projects
None yet
Development

No branches or pull requests

3 participants