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

Replace AbstractEngineProcessorShim #4842

Merged
merged 34 commits into from
Feb 11, 2022
Merged

Conversation

mpharrigan
Copy link
Collaborator

@mpharrigan mpharrigan commented Jan 14, 2022

We were using AbstractEngineProcessorShim pending #4644 and friends. Those PRs have landed. For data hygiene as part of the workflow tools, we need to record what processor was used. This PR introduces an abstract ProcessorRecord interface and three implementations for mapping (processor_id, whether it's a simulator, what sort of noise) tuples to an AbstractProcessor instance for doing the work.

  • Remove AbstractEngineProcessorShim
  • Add dataclasses for data hygene
  • Now we can have proper serialization tests for anything that saves a QuantumRuntimeConfiguration, hence the new json files
  • We don't need to shim over cirq.read_json in tests that make use of AbstractEngineProcessorShim any more
  • Side effect: the new engine-processor-backed instances mean we have to use "real" qubits in the tests, so there are some knock-on test changes.

cc @dstrain115 @MichaelBroughton

@mpharrigan mpharrigan requested review from cduck, vtomole, wcourtney and a team as code owners January 14, 2022 19:37
@mpharrigan mpharrigan requested a review from viathor January 14, 2022 19:38
@CirqBot CirqBot added the size: M 50< lines changed <250 label Jan 14, 2022
@mpharrigan
Copy link
Collaborator Author

I don't think we can use SimulatedLocalProcessor directly because it isn't serializable -- either philosophically or in practice.

@mpharrigan
Copy link
Collaborator Author

related: #2820

@mpharrigan mpharrigan changed the title Remove AbstractEngineProcessorShim Replace AbstractEngineProcessorShim Jan 26, 2022
@mpharrigan
Copy link
Collaborator Author

@MichaelBroughton @dstrain115 this should be ready for your review, PTAL

def get_processor(self) -> 'cg.EngineProcessor':
"""Return a `cg.EngineProcessor` for the specified processor_id."""
engine = cg.get_engine()
return cg.EngineProcessor(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not just engine.get_processor(self.processor_id)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah. (1) I didn't think to do that; (2) I thought I was going to have to put in more environment variable magic into this function that turned out not to be necessary.

changed as suggested.

This implementation hardcodes the `cg.SQRT_ISWAP_GATESET` to construct
the sampler until this argument is made optional.
"""
return self.get_processor().get_sampler(cg.SQRT_ISWAP_GATESET)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should the default be the CircuitSerializer? It's more general and can handle the union of the other gatesets (at the cost of being less tested, but please use this to help test it!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh wow. when was that merged in? will test it out

Only 'rainbow' and 'weber' are recognized processor_ids and the device information
may not be up-to-date, as it is completely local.
"""
return _DEVICES_BY_ID[self.processor_id]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want to use the devices in devices/specifications?

For instance, there is this call which is very similar:
create_noiseless_virtual_engine_from_latest_templates

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes I do.

I'm going to try to see what I have to add to that module so I can request the latest template by name instead of getting them in a list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I'd need something like #4909 to re-use as much as possible

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

merged in #4909 to use virtual_engine_factory stuff to make these local devices

@dstrain115
Copy link
Collaborator

Also, did #4850 solve your optional gate set issues? Or is there further work needed?

from cirq._compat import dataclass_repr
from cirq_google.engine.virtual_engine_factory import (
MOST_RECENT_TEMPLATES,
_create_device_spec_from_template,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this function not have a _ prefix if it's imported here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm I think since it's all inside cirq_google it's fine.

Do you think it should be a public method? Easier to make public later than vice versa

Copy link
Collaborator

Choose a reason for hiding this comment

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

My impression is that _ implies it's private within a module, but I'm not a Python guru so not a strong preference :) According to The naming guide from "PEP 8 -- Style Guide for Python Code":

"_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose names start with an underscore."

This seems to imply that objects used outside a module shouldn't have an underscore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that still depends on your interpretation of the meaning of "internal", right? module internal or package internal. As an example, we have cirq._compat that's used throughout cirq but external users should not import.

In any event, if folks (i.e. you) think this is a valuable method for users to use, we can make it "public"

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK sounds good, no strong preference from me on whether to have _. As for whether we should make it available for users to use, I'd say not until there's a specific need. So keeping the underscore sgtm.

engine = cg.get_engine()
return engine.get_processor(self.processor_id)

def get_sampler(self) -> 'cg.QuantumEngineSampler':
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need to be reimplemented if the abstract class logic is the same?

Is the more specific cg.QuantumEngineSampler type used anywhere over the cirq.Sampler?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch. this was vestigial from when I had to hack in a gateset

"""Return a `cg.QuantumEngineSampler` for the specified processor_id."""
return self.get_processor().get_sampler()

def get_device(self) -> 'cirq.Device':
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

CirqBot pushed a commit that referenced this pull request Feb 10, 2022
- Factor out `create_noiseless_virtual_processor_from_proto` for use in `create_noiseless_virtual_engine_from_proto`
- Factor out `_create_device_spec_from_template` for use in `create_noiseless_virtual_processor_from_template` and `create_noiseless_virtual_engine_from_templates`
- Factor out `MOST_RECENT_TEMPLATES` for use in `create_noiseless_virtual_engine_from_latest_templates` and (manually) in `create_noiseless_virtual_processor_from_template`

This unblocks creating one processor at a time for use in #4842
@mpharrigan
Copy link
Collaborator Author

@verult @dstrain115 ptal. With #4909 merged in, the diff should be fixed (smaller)

Copy link
Collaborator

@verult verult left a comment

Choose a reason for hiding this comment

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

Did a surface-level review and lgtm. Please feel free to ignore nits.

def _json_dict_(self):
return cirq.dataclass_json_dict(self)

def descriptive_name(self) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Should this be __str__()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

},
clear=True,
):
proc_rec = cg.EngineProcessorRecord('rainbow')
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: declare processor ID as a constant at top of file. Makes it easier to change when we remove device templates in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

gonna definitely keep it 1) to keep the tests descriptive and readable 2) to make sure we don't remove device templates in the future!

Copy link
Collaborator

@verult verult left a comment

Choose a reason for hiding this comment

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

Approving per discussion in chat

@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Feb 11, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Feb 11, 2022
@CirqBot CirqBot merged commit 7895028 into quantumlib:master Feb 11, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Feb 11, 2022
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
- Factor out `create_noiseless_virtual_processor_from_proto` for use in `create_noiseless_virtual_engine_from_proto`
- Factor out `_create_device_spec_from_template` for use in `create_noiseless_virtual_processor_from_template` and `create_noiseless_virtual_engine_from_templates`
- Factor out `MOST_RECENT_TEMPLATES` for use in `create_noiseless_virtual_engine_from_latest_templates` and (manually) in `create_noiseless_virtual_processor_from_template`

This unblocks creating one processor at a time for use in quantumlib#4842
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
We were using `AbstractEngineProcessorShim` pending quantumlib#4644 and friends. Those PRs have landed. For data hygiene as part of the workflow tools, we need to record what processor was used. This PR introduces an abstract `ProcessorRecord` interface and three implementations for mapping (processor_id, whether it's a simulator, what sort of noise) tuples to an AbstractProcessor instance for doing the work.

 - Remove `AbstractEngineProcessorShim`
 - Add dataclasses for data hygene
 - Now we can have proper serialization tests for anything that saves a `QuantumRuntimeConfiguration`, hence the new json files
 - We don't need to shim over `cirq.read_json` in tests that make use of `AbstractEngineProcessorShim` any more
 - Side effect: the new engine-processor-backed instances mean we have to use "real" qubits in the tests, so there are some knock-on test changes.

cc @dstrain115 @MichaelBroughton
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
- Factor out `create_noiseless_virtual_processor_from_proto` for use in `create_noiseless_virtual_engine_from_proto`
- Factor out `_create_device_spec_from_template` for use in `create_noiseless_virtual_processor_from_template` and `create_noiseless_virtual_engine_from_templates`
- Factor out `MOST_RECENT_TEMPLATES` for use in `create_noiseless_virtual_engine_from_latest_templates` and (manually) in `create_noiseless_virtual_processor_from_template`

This unblocks creating one processor at a time for use in quantumlib#4842
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
We were using `AbstractEngineProcessorShim` pending quantumlib#4644 and friends. Those PRs have landed. For data hygiene as part of the workflow tools, we need to record what processor was used. This PR introduces an abstract `ProcessorRecord` interface and three implementations for mapping (processor_id, whether it's a simulator, what sort of noise) tuples to an AbstractProcessor instance for doing the work.

 - Remove `AbstractEngineProcessorShim`
 - Add dataclasses for data hygene
 - Now we can have proper serialization tests for anything that saves a `QuantumRuntimeConfiguration`, hence the new json files
 - We don't need to shim over `cirq.read_json` in tests that make use of `AbstractEngineProcessorShim` any more
 - Side effect: the new engine-processor-backed instances mean we have to use "real" qubits in the tests, so there are some knock-on test changes.

cc @dstrain115 @MichaelBroughton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants