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

get_engine_sampler #2767

Merged
merged 8 commits into from
Jul 17, 2020
Merged

get_engine_sampler #2767

merged 8 commits into from
Jul 17, 2020

Conversation

mpharrigan
Copy link
Collaborator

Remove tons of boilerplate

Remove tons of boilerplate
@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Feb 13, 2020
Copy link
Collaborator

@vtomole vtomole left a 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..

@mpharrigan
Copy link
Collaborator Author

hmm I'll have to dig into the mac ci

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'],
Copy link
Contributor

@maffoo maffoo Feb 20, 2020

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 :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. Actually yeah, I should add an argument that defaults to the environment variable
  2. 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added!

@mpharrigan
Copy link
Collaborator Author

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,
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

Copy link
Collaborator

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().

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 thought we were discouraging (if not deprecating) engine.run as it does not follow the sampler api

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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.

@kevinsung kevinsung dismissed their stale review May 11, 2020 19:44

ok with string

document(XMON, """Gate set for XMON devices.""")

NAMED_GATESETS = {
'sqrt-iswap': 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.

Strange that these names don't match the gate set names (which are used when they are serialized).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

Copy link
Collaborator

@dabacon dabacon left a 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,
Copy link
Collaborator

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.

"""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.
Copy link
Collaborator

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.

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

project_id, unless set explicitly. We assume you are using ProtoVersion.V2.

Args:
processor_id: Engine processor ID (from cloud console)
Copy link
Collaborator

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.

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

@mpharrigan
Copy link
Collaborator Author

I merged master and noted there's a new global variable GOOGLE_GATESETS that was missing the new "fsim" gateset; which I also added to this function

@mpharrigan
Copy link
Collaborator Author

We good?

@balopat balopat added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 17, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 17, 2020
@CirqBot CirqBot merged commit 8ad5186 into quantumlib:master Jul 17, 2020
@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 Jul 17, 2020
@mpharrigan mpharrigan deleted the get-engine-sampler branch July 20, 2020 18:55
CirqBot pushed a commit that referenced this pull request Jul 22, 2020
#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.
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Aug 23, 2020
Remove tons of boilerplate
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Aug 23, 2020
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants