@@ -23,6 +23,10 @@ gcloud client:
2323
2424From a colab, you can execute:
2525
26+ <!-- -test_substitution
27+ from google\.colab import auth
28+ auth = mock.MagicMock()
29+ --->
2630``` python
2731from google.colab import auth
2832auth.authenticate_user(clear_output = False )
@@ -41,42 +45,52 @@ You can use this instance to run quantum circuits or sweeps (parameterized
4145variants of a general circuit).
4246
4347<!-- -test_substitution
44- results = job.results.*
45- results = None
46- --->
47- <!-- -test_substitution
48- print.results.idx.*
49- print()
48+ engine = cirq_google.Engine\b.*
49+ # This pattern matches in all snippets.
50+ # We repeat the expression first to ensure Engine argumets are correct.
51+ \g<0>
52+ # Then we create various mock objects that derive from engine calls.
53+ engine = mock_engine = mock.create_autospec(cirq_google.Engine, instance=True)
54+ mock_sampler = mock.create_autospec(cirq.Sampler, instance=True)
55+ mock_engine_job = mock.create_autospec(cirq_google.EngineJob, instance=True,
56+ program_id="program_id", job_id="job_id")
57+ mock_engine_program = mock.create_autospec(cirq_google.EngineProgram, instance=True,
58+ program_id="program_id")
59+ # Finally let us import datetime for snippets that use it below
60+ import datetime
5061--->
5162<!-- -test_substitution
52- engine = cirq_google.Engine(.*)
53- engine = MockEngine()
63+ sampler = engine.get_sampler.processor_id=PROCESSOR_ID.*
64+ mock_engine.configure_mock(**{"get_sampler.return_value": mock_sampler})
65+ \g<0>
5466--->
5567<!-- -test_substitution
56- cg.Engine(.*)
57- cirq.Simulator()
68+ results = sampler.run\b
69+ mock_result = mock.create_autospec(cirq.Result, instance=True)
70+ mock_sampler.configure_mock(**{"run.return_value": mock_result})
71+ \g<0>
5872--->
5973<!-- -test_substitution
60- sampler = .*
61- sampler = engine
74+ PROJECT_ID|PROCESSOR_ID
75+ 'placeholder'
6276--->
6377``` python
6478import cirq
65- import cirq_google as cg
79+ import cirq_google
6680
6781# A simple sample circuit
6882qubit = cirq.GridQubit(5 , 2 )
6983circuit = cirq.Circuit(
70- cirq.X(qubit)** 0.5 , # Square root of NOT.
71- cirq.measure(qubit, key = ' result' ) # Measurement.
84+ cirq.X(qubit) ** 0.5 , # Square root of NOT.
85+ cirq.measure(qubit, key = ' result' ), # Measurement.
7286)
7387
7488# Create an Engine object.
75- # Replace YOUR_PROJECT_ID with the id from your cloud project.
76- engine = cg .Engine(project_id = YOUR_PROJECT_ID )
89+ # Replace PROJECT_ID with the id from your cloud project.
90+ engine = cirq_google .Engine(project_id = PROJECT_ID )
7791
7892# Create a sampler from the engine
79- sampler = engine.get_sampler(processor_id = ' PROCESSOR_ID' )
93+ sampler = engine.get_sampler(processor_id = PROCESSOR_ID )
8094
8195# This will run the circuit and return the results in a 'Result'
8296results = sampler.run(circuit, repetitions = 1000 )
@@ -152,26 +166,36 @@ Currently, getting the program and job ids can only be done through the
152166You can then use ` get_program ` and ` get_job ` to retrieve the results.
153167See below for an example:
154168
169+ <!-- -test_substitution
170+ job = engine.run_sweep.program=circuit
171+ mock_engine.configure_mock(**{
172+ "run_sweep.return_value": mock_engine_job,
173+ "get_program.return_value": mock_engine_program,
174+ })
175+ mock_engine_program.configure_mock(**{"get_job.return_value": mock_engine_job})
176+ \g<0>
177+ --->
155178``` python
156179# Initialize the engine object
157- engine = cirq_google.Engine(project_id = ' YOUR_PROJECT_ID ' )
180+ engine = cirq_google.Engine(project_id = PROJECT_ID )
158181
159182# Create an example circuit
160183qubit = cirq.GridQubit(5 , 2 )
161184circuit = cirq.Circuit(
162- cirq.X(qubit)** sympy.Symbol(' t' ),
163- cirq.measure(qubit, key = ' result' )
185+ cirq.X(qubit) ** sympy.Symbol(' t' ),
186+ cirq.measure(qubit, key = ' result' ),
164187)
165188param_sweep = cirq.Linspace(' t' , start = 0 , stop = 1 , length = 10 )
166189
167190# Run the circuit
168- job = e.run_sweep(program = circuit,
169- params = param_sweep,
170- repetitions = 1000 ,
171- processor_id = ' PROCESSOR_ID' ,
172- gate_set = GATE_SET )
191+ job = engine.run_sweep(
192+ program = circuit,
193+ params = param_sweep,
194+ repetitions = 1000 ,
195+ processor_id = PROCESSOR_ID ,
196+ )
173197
174- # Save the program and jo id for later
198+ # Save the program and job id for later
175199program_id = job.program_id
176200job_id = job.job_id
177201
@@ -187,7 +211,6 @@ historical_job = engine.get_program(program_id=program_id).get_job(job_id=job_id
187211
188212# Retrieve the results
189213historical_results = historical_job.results()
190-
191214```
192215
193216If you did not save the ids, you can still find them from your
@@ -200,17 +223,24 @@ by using our list methods.
200223To list the executions of your circuit, i.e., the jobs, you can use ` cirq_google.Engine.list_jobs() ` .
201224You can search in all the jobs within your project using filtering criteria on creation time, execution state and labels.
202225
226+ <!-- -test_substitution
227+ jobs = engine.list_jobs.created_after=datetime.date.*
228+ mock_engine.configure_mock(**{"list_jobs.return_value": [mock_engine_job]})
229+ \g<0>
230+ --->
203231``` python
204- from cirq_google.engine.client.quantum import enums
232+ from cirq_google.cloud import quantum
205233
206234# Initialize the engine object
207- engine = cirq_google.Engine(project_id = ' YOUR_PROJECT_ID ' )
235+ engine = cirq_google.Engine(project_id = PROJECT_ID )
208236
209237# List all the jobs on the project since 2020/09/20 that succeeded:
210- jobs = engine.list_jobs(created_after = datetime.date(2020 ,9 ,20 ),
211- execution_states = [enums.ExecutionStatus.State.SUCCESS ])
238+ jobs = engine.list_jobs(
239+ created_after = datetime.date(2020 , 9 , 20 ),
240+ execution_states = [quantum.ExecutionStatus.State.SUCCESS ],
241+ )
212242for j in jobs:
213- print (j.job_id, j.status(), j.create_time())
243+ print (j.job_id, j.status(), j.create_time())
214244```
215245
216246### Listing programs
@@ -219,23 +249,29 @@ To list the different instances of your circuits uploaded, i.e., the programs, y
219249Similar to jobs, filtering makes it possible to list programs by creation time and labels.
220250With an existing ` cirq_google.EngineProgram ` object, you can list any jobs that were run using that program.
221251
252+ <!-- -test_substitution
253+ programs = engine.list_programs[(].*
254+ mock_engine.configure_mock(**{"list_programs.return_value": [mock_engine_program]})
255+ mock_engine_program.configure_mock(**{"list_jobs.return_value": [mock_engine_job]})
256+ \g<0>
257+ --->
222258``` python
223- from cirq_google.engine.client.quantum import enums
259+ from cirq_google.cloud import quantum
224260
225261# Initialize the engine object
226- engine = cirq_google.Engine(project_id = ' YOUR_PROJECT_ID ' )
262+ engine = cirq_google.Engine(project_id = PROJECT_ID )
227263
228264# List all the programs on the project since 2020/09/20 that have
229265# the "variational" label with any value and the "experiment" label
230266# with value "vqe001":
231267programs = engine.list_programs(
232- created_after = datetime.date(2020 ,9 , 20 ),
233- has_labels = {" variational" :" *" , " experiment" :" vqe001" }
234- )
268+ created_after = datetime.date(2020 , 9 , 20 ),
269+ has_labels = {" variational" : " *" , " experiment" : " vqe001" },
270+ )
235271for p in programs:
236- print (p.program_id, p.create_time())
237- # the same filtering parametrization is available as in engine.list_jobs()
238- # for example here we list the jobs under the programs that failed
239- for j in p.list_jobs(execution_states = [enums .ExecutionStatus.State.FAILURE ]):
240- print (j.job_id, j.status())
272+ print (p.program_id, p.create_time())
273+ # the same filtering parametrization is available as in engine.list_jobs()
274+ # for example here we list the jobs under the programs that failed
275+ for j in p.list_jobs(execution_states = [quantum .ExecutionStatus.State.FAILURE ]):
276+ print (j.job_id, j.status())
241277```
0 commit comments