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

Qiskit Pulse - Introduce PulseIR #11021

Closed
wants to merge 37 commits into from
Closed

Qiskit Pulse - Introduce PulseIR #11021

wants to merge 37 commits into from

Conversation

TsafrirA
Copy link
Collaborator

Summary

This PR is the second step in implementing RFC0012. It the introduces the intermediate representation which will be user by the Qiskit Pulse Compiler.

This PR depends on #10694

Details and comments

For more details, see the RFC.

  • Recursion options were included only in some operations, with the default changing between True and False. I tried to pick the option I thought would be more useful, but the final product might be confusing.
  • Do we want a general documentation module for this? On the one hand, the IR is an internal tool, but on the other hand, vendors and users might want to add\modify the compiler behavior.

Release notes will be added at a later stage for the combined changes.

TsafrirA and others added 30 commits August 23, 2023 11:20
@TsafrirA TsafrirA requested review from eggerdj, wshanks and a team as code owners October 16, 2023 11:07
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @nkanazawa1989

@coveralls
Copy link

coveralls commented Oct 16, 2023

Pull Request Test Coverage Report for Build 6637721141

  • 264 of 268 (98.51%) changed or added relevant lines in 3 files are covered.
  • 12 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.03%) to 86.949%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/pulse/ir/ir_instructions.py 108 110 98.18%
qiskit/pulse/ir/pulse_ir.py 153 155 98.71%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 91.41%
qiskit/pulse/library/waveform.py 3 93.75%
crates/qasm2/src/parse.rs 6 97.6%
Totals Coverage Status
Change from base Build 6631999916: 0.03%
Covered Lines: 74233
Relevant Lines: 85375

💛 - Coveralls

Copy link
Contributor

@taalexander taalexander left a comment

Choose a reason for hiding this comment

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

Some initial comments are included in this review.

One component that is unclear to me is what is the underlying hardware resources? Ie see that we can have Qubits and Couplers which define/connect qubits and presumably are helpful for lowering to the underlying hardware resources.

However, frames accept these. My question is what does the resolution of the underlying hardware resource tie to which is ultimately need to connect to an instrument?

It seems we would need a Port/Channel object to which the frames would tie to. Giving us:

  1. Qubit/coupler
  2. frame
  3. port/channel on hardware (resolved internally)

Otherwise, I wonder what might happen for say experiments with multiple defined user frames and resolving them to hardware?

@@ -0,0 +1,335 @@
# This code is part of Qiskit.
Copy link
Contributor

Choose a reason for hiding this comment

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

These are already in the namespace and likely should be just ir/instructions.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are already several Instruction types in Qiskit, one of them in Pulse. On the one hand it might be more confusing, on the other hand, we already resolved all of the ambiguous referencing issues. @nkanazawa1989 , what do you think?

"""

@abstractmethod
def __init__(self, duration: int, operand, initial_time: Optional[int] = None):
Copy link
Contributor

Choose a reason for hiding this comment

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

In the long run you might want to consider typing duration other than int to enable reasoning about pulses of say different units and providing automated transformation routines at compile time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you thought about separating the concerns of the instruction and its timing information within a larger schedule? Having a higher-level structure that encapsulated the notion of time outside (or via attribution) to the instructions which would lead to better separation of concerns?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because scheduling is such a major part of the compiler's job, I think it's not very productive to separate this issue. What is the benefit you see in separating this?

Comment on lines +125 to +137
* Play
* Delay
* SetFrequency
* ShiftFrequency
* SetPhase
* ShiftPhase

Instructions are defined on both ``LogicalElement`` and ``Frame`` or in some cases only
one of the two. If only one of ``LogicalElement`` and ``Frame`` are provided, the instruction
is called partial instruction, and will require broadcasting during compilation.
"""

allowed_types = ["Play", "Delay", "SetFrequency", "ShiftFrequency", "SetPhase", "ShiftPhase"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Defining these types within the base type leads to a cyclical relationship and weak typing. It would be better to use subclassing within common values for each class passed to the initializer, ie.,

class NewType
    def __init__(
        self,
        "new_type",
        ...
    ):

specifying allowed_types is arbitrarily constraining.

Comment on lines +185 to +221
if instruction_type not in self.__class__.allowed_types:
raise PulseError(f"{instruction_type} is not a recognized instruction")

if instruction_type == "Delay":
if not isinstance(operand, (int, np.integer)) or operand < 0:
raise PulseError(
"The operand of a Delay instruction must be a non-negative integer."
)
if logical_element is None:
raise PulseError("Delay instruction must have an associated logical element.")
duration = operand

elif instruction_type == "Play":
if not isinstance(operand, (SymbolicPulse, Waveform)):
raise PulseError(
f"Play instruction is incompatible with operand of type {type(operand)}."
)
if logical_element is None or frame is None:
raise PulseError(
"Play instruction must have an associated logical element and frame."
)
duration = operand.duration

elif instruction_type in ["SetFrequency", "ShiftFrequency", "SetPhase", "ShiftPhase"]:
if isinstance(operand, (int, np.integer)):
operand = float(operand)
if not isinstance(operand, float):
raise PulseError(
"The operand of a Set/Shift Frequency/Phase instruction must be a float."
)
if frame is None:
raise PulseError(
"Set/Shift Frequency/Phase instruction must have an associated frame."
)
duration = 0

return duration
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic should be handled by subclassing and not baked into the core class. The need for it is a codesmell that this individual class knows too much.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. I'll change that.

Comment on lines +278 to +284
def __init__(
self,
qubit: Qubit,
memory_slot: MemorySlot,
duration: int,
initial_time: Optional[int] = None,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are acquires on qubits? This seems to be mixing representations, ie., at the pulse level there should be no knowledge of qubits since this is what defines a qubit.

Comment on lines +44 to +54
class PulseIR:
"""
``PulseIR`` is the backbone of the intermediate representation used in the Qiskit Pulse compiler.
A pulse program is represented as a single ``PulseIR`` object, with elements
which include IR instructions (base class :class:`~.BaseIRInstruction) and other
nested ``PulseIR`` objects. This structure mimics that of :class:`.qiskit.pulse.ScheduleBlock`
which is the main pulse program format used in Qiskit.

The IR is ment to support compilation needs, including partial instructions broadcasting,
scheduling, validation and more.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a module, a program, a sequence, a block? I believe the name PulseIR could be more descriptive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. I will change that.

Comment on lines +86 to +89
@property
def elements(self) -> List[Union[GenericInstruction, AcquireInstruction, "PulseIR"]]:
"""Return the elements of the ``PulseIR`` object"""
return self._elements
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like there should be a generic type returned that encompasses all of these rather than a list of types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. I will change that.

Comment on lines +294 to +313
def get_acquire_instructions(
self, qubit: Optional[Qubit] = None, recursive: Optional[bool] = True
) -> List[GenericInstruction]:
"""Return ``AcquireInstruction``\\ s

Args:
qubit: Optionally return only instructions associated with this qubit.
Default value - None (return all instructions).
recursive: If ``True`` recursively looks for instructions,
else ignores instructions of child ``PulseIR``. Default - ``True``.
"""
instructions = []
for element in self._elements:
if isinstance(element, AcquireInstruction) and (
qubit is None or element.qubit == qubit
):
instructions.append(element)
elif recursive and isinstance(element, PulseIR):
instructions.extend(element.get_acquire_instructions(qubit, recursive=recursive))
return instructions
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like we should provide generic means for getting instructions, not fixed routines for each function?

Comment on lines +294 to +295
def get_acquire_instructions(
self, qubit: Optional[Qubit] = None, recursive: Optional[bool] = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question about why qubits are involved with pulse?

Comment on lines 75 to 90
from .logical_elements import (
LogicalElement,
Qubit,
Coupler,
)

from .frames import (
Frame,
GenericFrame,
QubitFrame,
MeasurementFrame,
)

from .mixed_frames import (
MixedFrame,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me these are all IR components as well and should be tied in a common representation. Ie., within MLIR these would be types generated by operations and seem to be treated as such within frames.py.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you clarify what you mean by "generated by operations"?

Because these objects double as both the IR and the user interface, we wanted to define them separately from the IR.

@nkanazawa1989 nkanazawa1989 self-assigned this Oct 24, 2023
@nkanazawa1989 nkanazawa1989 added the mod: pulse Related to the Pulse module label Oct 24, 2023
@eliarbel eliarbel mentioned this pull request Oct 25, 2023
9 tasks
@nkanazawa1989 nkanazawa1989 linked an issue Oct 26, 2023 that may be closed by this pull request
9 tasks
@nkanazawa1989 nkanazawa1989 added the experimental Experimental feature without API stability guarantee label Jan 9, 2024
@TsafrirA
Copy link
Collaborator Author

Superseded by #11767.

@TsafrirA TsafrirA closed this Feb 11, 2024
@TsafrirA TsafrirA deleted the PulseIR branch March 13, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental Experimental feature without API stability guarantee mod: pulse Related to the Pulse module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pulse Compiler and IR
5 participants