-
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
get_engine_sampler #2767
get_engine_sampler #2767
Conversation
Remove tons of boilerplate
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.
Something about those creds are breaking the Macos check..
hmm I'll have to dig into the mac ci |
cirq/google/engine/engine_sampler.py
Outdated
raise ValueError(f"Please use one of the following gateset names: " | ||
f"{sorted(gate_sets.NAMED_GATESETS.keys())}") | ||
|
||
return engine.Engine(project_id=os.environ['GOOGLE_CLOUD_PROJECT'], |
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.
FWIW, in situations such as this I like to add an arg so the environment dict can be injected in tests, rather than monkey-patching:
def get_engine_sampler(processor_id: str, gate_set_name: str, env: Dict[str, str] = os.environ):
...
return engine.Engine(project_id=env['GOOGLE_CLOUD_PROJECT'], ...)
# in tests:
get_engine_sampler('foo', 'gates', {'GOOGLE_CLOUD_PROJECT': 'my-project'})
Some people are not too opposed to monkey-patching, but it always scares me :-)
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.
- Actually yeah, I should add an argument that defaults to the environment variable
- I'm going to leave the monkey patching test in as well. I think the primary utility of this method is dispatching to the environment variable for project id so people can share code that doesn't have their personal project id hardcoded
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.
added!
Anyone else have an opinion on this? |
@@ -64,3 +64,34 @@ def run_sweep( | |||
@property | |||
def engine(self) -> 'cirq.google.Engine': | |||
return self._engine | |||
|
|||
|
|||
def get_engine_sampler(processor_id: str, gate_set_name: str, |
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 that instead of taking a gate set name and having the NAMED_GATESETS dictionary, we should just take the gate set directly. There's no reason to add another layer of translation.
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.
at the risk of angering @dstrain115 by invoking the following argument: it's more pythonic to expose enum's using strings, especially in a user-facing api. See e.g. many numpy functions. In this case there are several advantages
- the gateset can be specified by command line arg and/or environment variable
- you don't have to find the correct import to find the gateset objects
- gate_set_name can live comfortably in a pandas dataframe or database table
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.
What is pythonic anyway?!
Having a string for this is inconsistent with other methods that we already have, such as engine.run().
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 thought we were discouraging (if not deprecating) engine.run
as it does not follow the sampler api
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 sampler api isn't flexible enough IMO to replace run
. It requires that you stick any extra information about your run into the sampler constructor, so if you really do care about this information you will end up creating samplers all over the place.
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.
Matt gave some good reasons to make it a string, so I'm fine with that. But we should be consistent.
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 only good reason I can think of not to use a string is that it makes it less extensible, but since there are only a set number of blessed gate sets, this seems fine to me.
cirq/google/gate_sets.py
Outdated
document(XMON, """Gate set for XMON devices.""") | ||
|
||
NAMED_GATESETS = { | ||
'sqrt-iswap': SQRT_ISWAP_GATESET, |
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.
Strange that these names don't match the gate set names (which are used when they are serialized).
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.
changed
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.
This seems useful and good to me. Think others agree strings are fine. Few minor comment.s
@@ -64,3 +64,34 @@ def run_sweep( | |||
@property | |||
def engine(self) -> 'cirq.google.Engine': | |||
return self._engine | |||
|
|||
|
|||
def get_engine_sampler(processor_id: str, gate_set_name: str, |
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 only good reason I can think of not to use a string is that it makes it less extensible, but since there are only a set number of blessed gate sets, this seems fine to me.
cirq/google/engine/engine_sampler.py
Outdated
"""Get an EngineSampler assuming some sensible defaults. | ||
|
||
This uses the environment variable GOOGLE_GLOUD_PROJECT for the Engine | ||
project_id, unless set explicitly. We assume you are using ProtoVersion.V2. |
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 we can remove comment about protoversion since no one should be using v1.
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/google/engine/engine_sampler.py
Outdated
project_id, unless set explicitly. We assume you are using ProtoVersion.V2. | ||
|
||
Args: | ||
processor_id: Engine processor ID (from cloud console) |
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.
or from Engine.list_processors
. Also nit: period to be consist with other args.
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
I merged master and noted there's a new global variable |
We good? |
#2767 introduced `get_engine_sampler`. This adds `get_engine` via a similar pattern, and in particular uses the same shared environment variable for projects, GOOGLE_PROJECT_ID. Also * deprecates `engine_from_environment` which used a different environment variable and a different nomenclature. It also fixes a bug in this methods id. * corrects a typo in the doc of `get_engine_sampler` * Gives the a sensible small string for an Engine object.
Remove tons of boilerplate
quantumlib#2767 introduced `get_engine_sampler`. This adds `get_engine` via a similar pattern, and in particular uses the same shared environment variable for projects, GOOGLE_PROJECT_ID. Also * deprecates `engine_from_environment` which used a different environment variable and a different nomenclature. It also fixes a bug in this methods id. * corrects a typo in the doc of `get_engine_sampler` * Gives the a sensible small string for an Engine object.
Remove tons of boilerplate