-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Convert cirq.Result to ABC and add ResultDict implementation #4806
Conversation
@daxfohl, FYI |
I think before doing this, let's make sure it's necessary. It may be possible that there's a unique internal representation that allows us to do everything we need, with high performance in all cases. Like perhaps if we model more closely what happens in hardware, that would be sufficient and performant for all the simulation logic as well. If that's the case then that one implementation would be sufficient and the abstraction would be unnecessary. (Not saying that there definitely is such a representation, just suggesting we should explore that approach first.) |
I think it's unlikely we'll have a single representation. At the very least, for data coming from hardware we sometimes have each measurement represented as a pair of I,Q values (if doing discrimination in software) and sometimes as a single bit (if doing discrimination onboard the control electronics). Most simulators would likely produce bits for measurements, so would never want to use the IQ representation. |
d91186c
to
e4799e0
Compare
I'll second this comment, with the added note that IQ points are specific to a particular subset of QC hardware designs. In order for Cirq to support IQ points and remain applicable to other hardware, multiple representations are all but required. |
That said, as implemented this is a breaking change. To avoid this we could instead name the abstract class
|
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.
Couple of smaller comments in addition to the breaking-change concern.
@@ -80,52 +82,35 @@ def _key_to_str(key: TMeasurementKey) -> str: | |||
return ','.join(str(q) for q in key) | |||
|
|||
|
|||
class Result: | |||
class Result(abc.ABC): |
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.
Update class docstring to reflect new behavior.
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.
Done.
cirq-core/cirq/study/result.py
Outdated
return ResultImpl(params=self.params, measurements=all_measurements) | ||
|
||
|
||
class ResultImpl(Result): |
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.
With an eye towards #3868, it may be preferable to name this implementation something that clearly indicates that its "default" storage type is keyed measurements (as opposed to raw values, pandas DataFrame, or something more exotic like IQ points).
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'm certainly open to renaming this, but not sure what a good name would be. Any specific suggestions?
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 simplest would be "KeyedResultImpl" or "MeasurementKeyResultImpl" - the others could then be e.g. "RawResultImpl" and "DataFrameResultImpl".
Re: backwards compatibility, I'm personally inclined to keep the simple name In addition, to ease the transition I've added a |
""" | ||
"""The results of multiple executions of a circuit with fixed parameters.""" | ||
|
||
def __new__(cls, *args, **kwargs): |
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.
Huh, this is a really neat workaround.
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 think I'm sold on the ResultImpl
vs AbstractResult
discussion, especially with the fix to avoid breaking Result(...)
calls. Left a comment on names for ResultImpl
, but other than that this is ready.
I've renamed the concrete implementation class |
The `cirq.Result` constructor is deprecated as of quantumlib/Cirq#4806. Instead, we should use a concrete type like `cirq.ResultDict`. This changes `StimSampler` to use `cirq.ResultDict` when available, to avoid deprecation warnings. I'm also curious to know what is the support policy for stimcirq supporting different pyle versions. This workaround can be removed whenever support for cirq < 0.14 is dropped.
…lib#4806) This converts cirq.Result into an abstract base class which defines the interface provided by measurement result objects. We introduce a new class cirq.ResultDict which is the default implementation of this result type, based on a dict mapping string keys to array data. This will allow us to provide alternate implementations as needed, for example a result type that is based on a DataFrame and only converts to measurement dict on demand (cf quantumlib#4774), or a result type that is based on experimental data (such as raw IQ data) and allows accessing this raw data while also converting to the standard measurements dict on demand (see quantumlib#3868 for other possible Result implementations). Making this separation will also simplify the implementation of repeated measurement keys (quantumlib#4274), since it will allow us to define the new interface for accessing data from repeated keys separately from the implementation of how this data is stored. Review: @95-martin-orion
…lib#4806) This converts cirq.Result into an abstract base class which defines the interface provided by measurement result objects. We introduce a new class cirq.ResultDict which is the default implementation of this result type, based on a dict mapping string keys to array data. This will allow us to provide alternate implementations as needed, for example a result type that is based on a DataFrame and only converts to measurement dict on demand (cf quantumlib#4774), or a result type that is based on experimental data (such as raw IQ data) and allows accessing this raw data while also converting to the standard measurements dict on demand (see quantumlib#3868 for other possible Result implementations). Making this separation will also simplify the implementation of repeated measurement keys (quantumlib#4274), since it will allow us to define the new interface for accessing data from repeated keys separately from the implementation of how this data is stored. Review: @95-martin-orion
This converts
cirq.Result
into an abstract base class which defines the interface provided by measurement result objects. We introduce a new classcirq.ResultImpl
which is the default implementation of this result type, based on a dict mapping string keys to array data. This will allow us to provide alternate implementations as needed, for example one could have a result that is based on a DataFrame and only converts to measurement dict on demand (cf #4774), or a result type that is based on experimental data (such as raw IQ data) and allows accessing this raw data while also converting to the standard measurements dict on demand (see #3868 for other possibleResult
implementations). Making this separation will also simplify the implementation of repeated measurement keys (#4274), since it will allow us to define the new interface for accessing data from repeated keys separately from the implementation of how this data is stored.