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

Fix handling of globally defined operations in Target #8925

Merged
merged 3 commits into from
Oct 18, 2022

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes the handling in the target for working with instructions defined globally in the target. It adds handling to the operation_names_for_qargs() and operations_for_qargs() methods to build outputs that include globally defined operations instead of ignoring them. Additionally the add_instruction() method is updated to validate the input qargs to ensure that only valid qubits and qargs are allowed to be added to the target for an instruction.

Details and comments

Fixes #8914

This commit fixes the handling in the target for working with
instructions defined globally in the target. It adds handling to the
operation_names_for_qargs() and operations_for_qargs() methods to build
outputs that include globally defined operations instead of ignoring
them. Additionally the add_instruction() method is updated to validate
the input qargs to ensure that only valid qubits and qargs are allowed
to be added to the target for an instruction.

Fixes Qiskit#8914
@mtreinish mtreinish added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels Oct 17, 2022
@mtreinish mtreinish added this to the 0.22.1 milestone Oct 17, 2022
@mtreinish mtreinish requested a review from a team as a code owner October 17, 2022 15:46
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Oct 17, 2022

Pull Request Test Coverage Report for Build 3269585468

  • 25 of 25 (100.0%) changed or added relevant lines in 2 files are covered.
  • 98 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.1%) to 84.623%

Files with Coverage Reduction New Missed Lines %
src/optimize_1q_gates.rs 1 95.16%
qiskit/extensions/quantum_initializer/squ.py 2 79.78%
src/sampled_exp_val.rs 14 63.93%
src/results/marginalization.rs 20 77.01%
src/sabre_swap/mod.rs 61 80.45%
Totals Coverage Status
Change from base Build 3269550990: -0.1%
Covered Lines: 61833
Relevant Lines: 73069

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the whole, this looks good to merge to me, and I'm glad it also handles variadic instructions correctly. My only comments are minor questions / gripes that don't actually need any action within this PR.

qiskit/transpiler/target.py Show resolved Hide resolved
qiskit/transpiler/target.py Show resolved Hide resolved
Comment on lines +356 to +364
if None in properties:
self._global_operations[instruction.num_qubits].add(instruction_name)
qargs_val = {}
for qarg in properties:
if qarg is not None and len(qarg) != instruction.num_qubits:
raise TranspilerError(
f"The number of qubits for {instruction} does not match the number "
f"of qubits in the properties dictionary: {qarg}"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably out-of-scope for this PR, but some minor griping: the logic flow of this function is getting a little tricky for me now, since when I originally read this, I thought it would cause bugs with variadic instructions, as you can't do type.num_qubits. In reality, it all works because properties is required to be {None: None}, so qarg is not None implies (transitively) that instruction.num_qubits should be valid, but because it's in a for qarg in properties loop, that's not so obvious to me any more.

Basically, I'm just griping that it's getting hard to keep all the different assumptions/implications about the objects in my head now. There's nothing actionable unless there's an obvious way to refactor the logic a little now that we've been forced to support variadic arguments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I feel like I need to write a big comment about all the different internal states and ways we track things. Part of it just is there are a lot of fiddly details here as there are basically 4 classes of instructions: normal instructions that have qubit restrictions either with or without properties, globally defined ideal instances of operations that have qubit and parameter restrictions, and globally defined variable width instructions. We've basically grafted the last two of those onto the target model which was designed for the first two (the 3rd class was originally just a shorthand to support ideal simulation without adding every permutation of qubit with no properties payload).

In this particular case I had tried to do this check outside the loop but I had logic flow issues with that as we still need to run this loop for the global fixed width instruction case even if this check itself isn't valid for those cases. I think we can look at trying to improve this in a separate refactor in a followup, especially as that I wouldn't want to backport necessarily.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's absolutely a follow-up thing, if we do it at all.

Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify mergify bot merged commit 3a073c7 into Qiskit:main Oct 18, 2022
mergify bot pushed a commit that referenced this pull request Oct 18, 2022
This commit fixes the handling in the target for working with
instructions defined globally in the target. It adds handling to the
operation_names_for_qargs() and operations_for_qargs() methods to build
outputs that include globally defined operations instead of ignoring
them. Additionally the add_instruction() method is updated to validate
the input qargs to ensure that only valid qubits and qargs are allowed
to be added to the target for an instruction.

Fixes #8914

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 3a073c7)
ajavadia pushed a commit to ajavadia/qiskit that referenced this pull request Oct 18, 2022
This commit fixes the handling in the target for working with
instructions defined globally in the target. It adds handling to the
operation_names_for_qargs() and operations_for_qargs() methods to build
outputs that include globally defined operations instead of ignoring
them. Additionally the add_instruction() method is updated to validate
the input qargs to ensure that only valid qubits and qargs are allowed
to be added to the target for an instruction.

Fixes Qiskit#8914

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
mergify bot added a commit that referenced this pull request Oct 18, 2022
This commit fixes the handling in the target for working with
instructions defined globally in the target. It adds handling to the
operation_names_for_qargs() and operations_for_qargs() methods to build
outputs that include globally defined operations instead of ignoring
them. Additionally the add_instruction() method is updated to validate
the input qargs to ensure that only valid qubits and qargs are allowed
to be added to the target for an instruction.

Fixes #8914

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 3a073c7)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no validity check on Target construction
5 participants