-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix transpile() for control flow operations with a Target/BackendV2 #8852
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c47f4ea
Fix transpile() for control flow operations with a Target/BackendV2
mtreinish 5053a53
Merge branch 'main' into target-controlflow-ops
mtreinish 8f2605e
Simplify test slightly
mtreinish 9d2d726
Merge remote-tracking branch 'origin/main' into target-controlflow-ops
mtreinish 905adc3
Add release note
mtreinish 730f04e
Add coupling map target test
mtreinish 6ce7fe6
Raise if instruction class and properties are both set
mtreinish e1b72bd
Only return global gates if they exist
mtreinish 07ed34f
Change UserWarning to a comment
mtreinish 17ee512
Revert change to instructions() getter
mtreinish ce74823
Merge remote-tracking branch 'origin/main' into target-controlflow-ops
mtreinish f06d1b8
Add release note about coupling map api change
mtreinish 746519b
Fix lint and logic bug
mtreinish 600c092
Add back nested while_loop to transpile() test
mtreinish ca5994d
Merge branch 'main' into target-controlflow-ops
mtreinish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
releasenotes/notes/fix-target-control-flow-representation-09520e2838f0657e.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| Add support for representing an operation that has a variable width | ||
| to the :class:`~.Target` class. Previously, a :class:`~.Target` object | ||
| needed to have an instance of :class:`~Operation` defined for each | ||
| operation supported in the target. This was used for both validation | ||
| of arguments and parameters of the operation. However, for operations | ||
| that have a variable width this wasn't possible because each instance | ||
| of an :class:`~Operation` class can only have a fixed number of qubits. | ||
| For cases where a backend supports variable width operations the | ||
| instruction can be added with the class of the operation instead of an | ||
| instance. In such cases the operation will be treated as globally | ||
| supported on all qubits. For example, if building a target like:: | ||
|
|
||
| from qiskit.transpiler import Target | ||
|
|
||
| ibm_target = Target() | ||
| i_props = { | ||
| (0,): InstructionProperties(duration=35.5e-9, error=0.000413), | ||
| (1,): InstructionProperties(duration=35.5e-9, error=0.000502), | ||
| (2,): InstructionProperties(duration=35.5e-9, error=0.0004003), | ||
| (3,): InstructionProperties(duration=35.5e-9, error=0.000614), | ||
| (4,): InstructionProperties(duration=35.5e-9, error=0.006149), | ||
| } | ||
| ibm_target.add_instruction(IGate(), i_props) | ||
| rz_props = { | ||
| (0,): InstructionProperties(duration=0, error=0), | ||
| (1,): InstructionProperties(duration=0, error=0), | ||
| (2,): InstructionProperties(duration=0, error=0), | ||
| (3,): InstructionProperties(duration=0, error=0), | ||
| (4,): InstructionProperties(duration=0, error=0), | ||
| } | ||
| ibm_target.add_instruction(RZGate(theta), rz_props) | ||
| sx_props = { | ||
| (0,): InstructionProperties(duration=35.5e-9, error=0.000413), | ||
| (1,): InstructionProperties(duration=35.5e-9, error=0.000502), | ||
| (2,): InstructionProperties(duration=35.5e-9, error=0.0004003), | ||
| (3,): InstructionProperties(duration=35.5e-9, error=0.000614), | ||
| (4,): InstructionProperties(duration=35.5e-9, error=0.006149), | ||
| } | ||
| ibm_target.add_instruction(SXGate(), sx_props) | ||
| x_props = { | ||
| (0,): InstructionProperties(duration=35.5e-9, error=0.000413), | ||
| (1,): InstructionProperties(duration=35.5e-9, error=0.000502), | ||
| (2,): InstructionProperties(duration=35.5e-9, error=0.0004003), | ||
| (3,): InstructionProperties(duration=35.5e-9, error=0.000614), | ||
| (4,): InstructionProperties(duration=35.5e-9, error=0.006149), | ||
| } | ||
| ibm_target.add_instruction(XGate(), x_props) | ||
| cx_props = { | ||
| (3, 4): InstructionProperties(duration=270.22e-9, error=0.00713), | ||
| (4, 3): InstructionProperties(duration=305.77e-9, error=0.00713), | ||
| (3, 1): InstructionProperties(duration=462.22e-9, error=0.00929), | ||
| (1, 3): InstructionProperties(duration=497.77e-9, error=0.00929), | ||
| (1, 2): InstructionProperties(duration=227.55e-9, error=0.00659), | ||
| (2, 1): InstructionProperties(duration=263.11e-9, error=0.00659), | ||
| (0, 1): InstructionProperties(duration=519.11e-9, error=0.01201), | ||
| (1, 0): InstructionProperties(duration=554.66e-9, error=0.01201), | ||
| } | ||
| ibm_target.add_instruction(CXGate(), cx_props) | ||
| measure_props = { | ||
| (0,): InstructionProperties(duration=5.813e-6, error=0.0751), | ||
| (1,): InstructionProperties(duration=5.813e-6, error=0.0225), | ||
| (2,): InstructionProperties(duration=5.813e-6, error=0.0146), | ||
| (3,): InstructionProperties(duration=5.813e-6, error=0.0215), | ||
| (4,): InstructionProperties(duration=5.813e-6, error=0.0333), | ||
| } | ||
| ibm_target.add_instruction(Measure(), measure_props) | ||
| ibm_target.add_instruction(IfElseOp, name="if_else") | ||
| ibm_target.add_instruction(ForLoopOp, name="for_loop") | ||
| ibm_target.add_instruction(WhileLoopOp, name="while_loop") | ||
|
|
||
| The :class:`~.IfElseOp`, :class:`~.ForLoopOp`, and :class:`~.WhileLoopOp` | ||
| operations are globally supported for any number of qubits. This is then | ||
| reflected by other calls in the :class:`~.Target` API such as | ||
| :meth:`~.Target.instruction_supported`:: | ||
|
|
||
| ibm_target.instruction_supported(operation_class=WhileLoopOp, qargs=(0, 2, 3, 4)) | ||
| ibm_target.instruction_supported('if_else', qargs=(0, 1)) | ||
|
|
||
| both return ``True``. | ||
| upgrade: | ||
| - | | ||
| For :class:`~.Target` objects that only contain globally defined 2 qubit | ||
| operations without any connectivity constaints the return from the | ||
| :meth:`.Target.build_coupling_map` method will now return ``None`` instead | ||
| of a :class:`~.CouplingMap` object that contains ``num_qubits`` nodes | ||
| and no edges. This change was made to better reflect the actual | ||
| connectivity constraints of the :class:`~.Target` because in this case | ||
| there are no connectivity constraints on the backend being modeled by | ||
| the :class:`~.Target`, not a lack of connecitvity. If you desire the | ||
| previous behavior for any reason you can reproduce it by checking for a | ||
| ``None`` and manually building a coupling map, for example:: | ||
|
|
||
| from qiskit.transpiler import Target | ||
| from qiskit.circuit.library import CXGate | ||
|
|
||
| target = Target(num_qubits=3) | ||
| target.add_instruction(CXGate()) | ||
| cmap = target.build_coupling_map() | ||
| if cmap is None: | ||
| cmap = CouplingMap() | ||
| for i in range(target.num_qubits): | ||
| cmap.add_physical_qubit(i) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.