Skip to content

Commit b96afa9

Browse files
Clean up snippet test errors in ionq and pasqal md files (#7918)
Similar to #7803, this commit fix the errors from the temporarily excluded md files. 1. Use mocked Service instance for ionq documents 2. Remove the outdated Circuit initialization in pasqal doc, the device argument has been removed since #5189. Fixes #7787 --------- Co-authored-by: Pavol Juhas <juhas@google.com>
1 parent 6689231 commit b96afa9

File tree

7 files changed

+66
-25
lines changed

7 files changed

+66
-25
lines changed

dev_tools/snippets_test.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,9 @@ def test_can_run_readme_code_snippets():
8585

8686

8787
def find_docs_code_snippets_paths() -> Iterator[str]:
88-
# TODO: #7787 - fix and enable these later
89-
excluded = (
90-
'hardware/ionq/access.md',
91-
'hardware/ionq/calibrations.md',
92-
'hardware/ionq/circuits.md',
93-
'hardware/ionq/jobs.md',
94-
'hardware/ionq/service.md',
95-
'hardware/pasqal/sampler.md',
96-
)
9788
for filename in DOCS_FOLDER.rglob('*.md'):
9889
path = filename.relative_to(DOCS_FOLDER).as_posix()
99-
if not path.endswith(excluded):
100-
yield path
90+
yield path
10191

10292

10393
@pytest.mark.parametrize('path', find_docs_code_snippets_paths())

docs/hardware/ionq/access.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export IONQ_API_KEY=tomyheart
3131
Once this variable is set, the `ionq.Service()` will look for it automatically
3232
in the environment:
3333

34+
<!---test_substitution
35+
service = ionq.Service\(\)
36+
import os
37+
with mock.patch.dict(os.environ, {'IONQ_API_KEY': 'tomyheart'}):
38+
\g<0>
39+
--->
3440
```python
3541
import cirq_ionq as ionq
3642

docs/hardware/ionq/calibrations.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ periodically.
77

88
In this section, we assume a `cirq_ionq.Service` object has been instantiated and is
99
called `service` and `cirq` and `cirq_ionq` have been imported:
10+
11+
<!---test_substitution
12+
service = ionq.Service\(\)
13+
service = mock.create_autospec(ionq.Service, instance=True)
14+
mock_calibration = mock.create_autospec(ionq.Calibration, instance=True)
15+
service.configure_mock(**{"get_current_calibration.return_value": mock_calibration})
16+
--->
1017
```python
1118
import cirq
1219
import cirq_ionq as ionq
20+
1321
service = ionq.Service()
1422
```
23+
1524
See [IonQ API Service](service.md) for how to set up the service.
1625

1726
## Fetching information about the device

docs/hardware/ionq/circuits.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ or on the IonQ simulator. Here we describe the restrictions on these circuits.
77

88
In this section we assume a `cirq_ionq.Service` object has been instantiated and is
99
called `service` and `cirq` and `cirq_ionq` have been imported:
10+
11+
<!---test_substitution
12+
service = ionq.Service\(\)
13+
service = mock.create_autospec(ionq.Service, instance=True)
14+
mock_calibration = mock.create_autospec(ionq.Calibration, instance=True)
15+
service.configure_mock(**{"get_current_calibration.return_value": mock_calibration})
16+
--->
1017
```python
1118
import cirq
1219
import cirq_ionq as ionq
20+
1321
service = ionq.Service()
1422
```
23+
1524
See [IonQ API Service](service.md) for how to set up the service.
1625

1726
## Qubits

docs/hardware/ionq/jobs.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ IonQ simulator.
66
In this section we assume a `cirq_ionq.Service` object has been instantiated and is
77
called `service` and `cirq` and `cirq_ionq` have been imported:
88

9+
<!---test_substitution
10+
service = ionq.Service\(\)
11+
service = mock.create_autospec(ionq.Service, instance=True)
12+
--->
913
```python
1014
import cirq
1115
import cirq_ionq as ionq
16+
1217
service = ionq.Service()
1318
```
1419

@@ -35,8 +40,8 @@ The first method for running is to do so via the `run` method on `cirq_ionq.Serv
3540
```python
3641
qubit = cirq.LineQubit(0)
3742
circuit = cirq.Circuit(
38-
cirq.X(qubit)**0.5, # Square root of NOT.
39-
cirq.measure(qubit, key='x') # Measurement store in key 'x'
43+
cirq.X(qubit) ** 0.5, # Square root of NOT.
44+
cirq.measure(qubit, key='x'), # Measurement store in key 'x'
4045
)
4146

4247
result = service.run(circuit=circuit, repetitions=100, target='qpu')
@@ -72,6 +77,12 @@ in a [pandas](https://pandas.pydata.org/) dataframe and is the interface
7277
used by other classes in Cirq for objects that process data. Here is a
7378
simple example showing how to get a sampler and use it.
7479

80+
<!---test_substitution
81+
sampler = service.sampler\(target=.*\)
82+
mock_sampler = mock.create_autospec(ionq.Sampler, instance=True)
83+
service.configure_mock(**{"sampler.return_value": mock_sampler})
84+
\g<0>
85+
--->
7586
```python
7687
qubit = cirq.LineQubit(0)
7788
circuit = cirq.Circuit(
@@ -93,6 +104,12 @@ wish to run, and the service immediately returns an object that has
93104
the id of the job. This job id can be recorded, and at any time in
94105
the future you can query for the results of this job.
95106

107+
<!---test_substitution
108+
job = service.create_job\(circuit=.*\)
109+
mock_job = mock.create_autospec(ionq.Job, instance=True)
110+
service.configure_mock(**{"create_job.return_value": mock_job})
111+
\g<0>
112+
--->
96113
```python
97114
qubit = cirq.LineQubit(0)
98115
circuit = cirq.Circuit(

docs/hardware/ionq/service.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ The basic steps for running a quantum circuit in a blocking manner are:
2121

2222
Here is a simple example of this flow:
2323

24+
<!---test_substitution
25+
service = ionq.Service\(api_key=API_KEY\)
26+
service = mock.create_autospec(ionq.Service, instance=True)
27+
mock_result = mock.create_autospec(cirq.Result, instance=True)
28+
service.configure_mock(**{"run.return_value": mock_result})
29+
--->
2430
```python
2531
import cirq
2632
import cirq_ionq as ionq
2733

2834
# A circuit that applies a square root of NOT and then a measurement.
2935
qubit = cirq.LineQubit(0)
3036
circuit = cirq.Circuit(
31-
cirq.X(qubit)**0.5, # Square root of NOT.
32-
cirq.measure(qubit, key='x') # Measurement store in key 'x'
37+
cirq.X(qubit) ** 0.5, # Square root of NOT.
38+
cirq.measure(qubit, key='x'), # Measurement store in key 'x'
3339
)
3440

3541
# Create a ionq.Service object.
@@ -105,11 +111,11 @@ Here is an example of using error mitigation and sharpening options:
105111
```python
106112
# Run a program against the service with error mitigation and sharpening
107113
result = service.run(
108-
circuit=circuit,
109-
repetitions=100,
110-
target='qpu',
111-
error_mitigation={'debias': True},
112-
sharpen=True
114+
circuit=circuit,
115+
repetitions=100,
116+
target='qpu',
117+
error_mitigation={'debias': True},
118+
sharpen=True,
113119
)
114120
```
115121

docs/hardware/pasqal/sampler.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ The `PasqalSampler` class is the entry point to communicate with the API. It can
1212
using your PASQAL_API_ACCESS_TOKEN.
1313

1414

15+
<!---test_substitution
16+
sampler = PasqalSampler\(.*\)
17+
PasqalSampler = mock.create_autospec(PasqalSampler)
18+
\g<0>
19+
--->
1520
```python
1621
import cirq
1722
from cirq_pasqal import ThreeDQubit, PasqalVirtualDevice, PasqalSampler
1823

1924
# A simple sample circuit
2025
qubit = ThreeDQubit(0, 0, 0)
21-
p_device = PasqalVirtualDevice(control_radius=2.1, qubits=[qubit])
22-
p_circuit = cirq.Circuit(device=p_device)
26+
p_circuit = cirq.Circuit()
2327
p_circuit.append(cirq.X(qubit)) # NOT gate.
2428
p_circuit.append(cirq.measure(qubit, key='result')) # Measurement.
2529

2630

2731
# Create a PasqalSampler object to use.
2832
# Replace 'my_token' with the access token and uncomment next lines.
2933

30-
# PASQAL_API_ACCESS_TOKEN = 'my_token'
31-
# sampler = cirq_pasqal.PasqalSampler(remote_host='http://34.98.71.118/v0/pasqal', access_token=PASQAL_API_ACCESS_TOKEN)
32-
# results = sampler.run(p_circuit, repetitions=1000) # Runs the circuit and returns the results in a 'Result'
34+
PASQAL_API_ACCESS_TOKEN = 'my_token'
35+
sampler = PasqalSampler(remote_host='http://34.98.71.118/v0/pasqal', access_token=PASQAL_API_ACCESS_TOKEN)
36+
results = sampler.run(p_circuit, repetitions=1000) # Runs the circuit and returns the results in a 'Result'
3337
```
3438

3539
## Device Specification

0 commit comments

Comments
 (0)