-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[WIP] Encapsulate instruction in args in "Instruction" class #7020
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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,81 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2021. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
""" | ||
Operation object | ||
""" | ||
|
||
from collections.abc import Sequence | ||
|
||
|
||
class Operation(Sequence): | ||
"""Class representation of a circuit operation | ||
|
||
An operation is an instruction with it's operands. | ||
""" | ||
|
||
__slots__ = ("instruction", "qargs", "cargs") | ||
|
||
def __init__(self, instruction, qargs=None, cargs=None): | ||
"""Initialize a new instruction object | ||
|
||
Args: | ||
instruction (qiskit.circuit.Instruction): The instruction object for | ||
the operation | ||
qargs (list): A list of :class:`~qiskit.circuit.Qubit` objects that | ||
the instruction runs on | ||
cargs (list): A list of :class:`~qiskit.circuit.Clbit` objects that | ||
the instruction runs on | ||
""" | ||
self.instruction = instruction | ||
if qargs is None: | ||
self.qargs = [] | ||
else: | ||
self.qargs = qargs | ||
if cargs is None: | ||
self.cargs = [] | ||
else: | ||
self.cargs = cargs | ||
|
||
def __len__(self): | ||
return 3 | ||
|
||
def __getitem__(self, index): | ||
if index == 0: | ||
return self.instruction | ||
if index == 1: | ||
return self.qargs | ||
if index == 2: | ||
return self.cargs | ||
if isinstance(index, slice): | ||
out_items = (self.instruction, self.qargs, self.cargs) | ||
return out_items.__getitem__(index) | ||
raise IndexError("Index %s is out of range" % index) | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, tuple): | ||
if other[0] == self.instruction and other[1] == self.qargs and other[2] == self.cargs: | ||
return True | ||
return False | ||
elif isinstance(other, Operation): | ||
if ( | ||
self.instruction == other.instruction | ||
and self.qargs == other.qargs | ||
and self.cargs == other.cargs | ||
): | ||
return True | ||
return False | ||
else: | ||
return False | ||
|
||
def __repr__(self): | ||
return repr((self.instruction, self.qargs, self.cargs)) |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The terminology we've used here has varied a bit, but it's probably worth thinking over before we nail this down.
Maybe someone with more compiler experience can weigh in, but I thought canonically an
Instruction
was the combination of anoperation
and itsoperands
(qargs and cargs), whereas with this PR we'd have anOperation
as the combination of anInstruction
and itsoperands
. Is there another name we want to use here, or are we set onOperation
? (We do already have anInstructionSet
which is ~largely unused but very similar in structure to what's here.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was under the impression that slotted attribute access was the fastest return and that's what I was optimizing for and my previous experience with named tuple was it's named attribute access was slower. However, as of py 3.8 that no longer seems to be the case (with this pr: python/cpython#10495 ). I did a quick test with this pr applied on python 3.9:
With indexed access:
with attribute access:
So I'm thinking the best choice is a named tuple despite my earlier hesitation (which was clearly wrong). I'll have to see how things behave in py3.7 and 3.6 too (although this is the last release with 3.6 support) but I doubt it will be an issue and even if it causes a regression I think it'll be an ok tradeoff (or we can wait until after 0.19 to do this and then we'll only support 3.7, 3.8, 3.9, and 3.10).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just seemed like the logical choice to me since we're already using instruction to represent the opcode. The terminology here is a bit fuzzy, like I've heard instructions and operation (well really opcode) used interchangeable. If you think operation here would be too overloaded the other term I was thinking of was
Statement
(ie one line in assembly). But I'll defer to what others like I don't feel too strongly as long as we're not reusing a name already used (since deprecatingInstruction
as it's used today isn't really a viable path).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think
Instruction
is the most appropriate name to refer to the tuple, which is like one line of qasm. It's unfortunate it wasn't used like that since the beginning. But there are places in qiskit where it's used like that, e.g.InstructionScheduleMap
.I was thinking in this release we can deprecate
Instruction
as it is now, and call it something else (actually Operation is a good name for that: https://en.wikipedia.org/wiki/Quantum_operation). And then re-introduceInstruction
in the next release to refer to the tuple (Operation, qubits, clbits). This would abide by our 3-month deprecation policy I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer
Instruction
for this class, andOperation
for the base class currently-known-as-Instruction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding
InstructionScheduleMap
, is it possible to wrap instruction to return parameters as a dictionaryDict[str, ParameterValue]
? This has been really problematic since parameters in a circuit instruction is order sensitive, and thus management of parameter in the mapper is not straightforward (because parameters in schedules are not order sensitive, i.e. this is always managed by dict). It seems to be good chance to address this issue.https://github.com/Qiskit/qiskit-terra/blob/66edc9d651f863be7fd195c5c9b40c5f610b3e03/qiskit/scheduler/lowering.py#L159
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a possible candidate to avoid the name collision around the re-use of
Instruction
, can we maybe re-use theInstructionSet
class here (and then, at some point in the future, renameInstructionSet
toInstruction
)?