Skip to content

Commit

Permalink
Use simpler argument style for V2 Sampler (Qiskit#1122)
Browse files Browse the repository at this point in the history
`SamplerV2.run()` has a flexible argument style. It always needs a list,
but each element can either be a single `QuantumCircuit` or it can be a
tuple like `(my_circuit,)` or `(my_circuit, None, 123)`.

These two lines are functionally equivalent:

```python
sampler.run([circuit1, circuit2])
```

```python
sampler.run([(circuit1,), (circuit2,)])
```

But the first one is simpler and easier to understand. So, this PR
consistently switches to that style. When we do need the tuple-syntax,
we now put each tuple on a dedicated line so that it's easier to see
each distinct tuple.
  • Loading branch information
Eric-Arellano authored Apr 4, 2024
1 parent 5c9338c commit 0a0bc66
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 21 deletions.
19 changes: 11 additions & 8 deletions docs/api/migration-guides/v2-primitives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ Sampler V2 example that uses shots in `run()`:

```python
# Sample two circuits at 128 shots each.
sampler.run([(circuit1,), (circuit2,)], shots=128)
sampler.run([circuit1, circuit2], shots=128)

# Sample two circuits at different amounts of shots. The "None"s are necessary
# as placeholders
# Sample two circuits at different amounts of shots.
# The "None"s are necessary as placeholders
# for the lack of parameter values in this example.
sampler.run([(circuit1, None, 123), (circuit2, None, 456)])
sampler.run([
(circuit1, None, 123),
(circuit2, None, 456),
])
```

#### Output
Expand Down Expand Up @@ -226,7 +229,7 @@ pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)

sampler = Sampler(backend)
job = sampler.run([(isa_circuit,)])
job = sampler.run([isa_circuit])
result = job.result()
# Get results for the first (and only) PUB
pub_result = result[0]
Expand Down Expand Up @@ -886,7 +889,7 @@ pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)

sampler = Sampler(backend)
job = sampler.run([(isa_circuit,)])
job = sampler.run([isa_circuit])
result = job.result()
```
</TabItem>
Expand Down Expand Up @@ -1109,8 +1112,8 @@ backend = service.least_busy(operational=True, simulator=False, min_num_qubits=1

with Session(service=service, backend=backend) as session:
sampler = Sampler(session=session)
job = sampler.run([(isa_circuit)])
another_job = sampler.run([(another_isa_circuit)])
job = sampler.run([isa_circuit])
another_job = sampler.run([another_isa_circuit])
result = job.result()
another_result = another_job.result()

Expand Down
2 changes: 1 addition & 1 deletion docs/build/bit-ordering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ decimal integer `2` (measured with probability `1.0`).
from qiskit.primitives import Samplerv2 as Sampler
qc.measure_all()

job = sampler.run([(qc)])
job = sampler.run([qc])
result = job.result()
print(f" > Counts: {result[0].data.meas.get_counts()}")
```
Expand Down
4 changes: 2 additions & 2 deletions docs/run/advanced-runtime-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ You can pass in options by using the `run()` method. This overwrites the options
In V2, the only options you can pass to `run()` are options defined in the interface. That is, `shots` for Sampler and `precision` for Estimator.
```python
# Sample two circuits at 128 shots each.
sampler.run([(circuit1,), (circuit2,)], shots=128)
sampler.run([circuit1, circuit2], shots=128)
```

</TabItem>
Expand Down Expand Up @@ -162,7 +162,7 @@ sampler.options.default_shots=2000
sampler.options.update(default_shots=1024, dynamical_decoupling={"sequence_type": "XpXm"})

# Sample two circuits at 128 shots each.
sampler.run([(circuit1,), (circuit2,)], shots=128)
sampler.run([circuit1, circuit2], shots=128)
```
</TabItem>

Expand Down
2 changes: 1 addition & 1 deletion docs/run/estimate-job-run-time.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ isa_circuit = pm.run(qc)
sampler = Sampler(backend)

# Submit the circuit to the sampler
job = sampler.run([(isa_circuit,)])
job = sampler.run([isa_circuit])

print(job.usage_estimation)
```
Expand Down
6 changes: 3 additions & 3 deletions docs/run/primitives-examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)

sampler = Sampler(backend)
job = sampler.run([(isa_circuit,)])
job = sampler.run([isa_circuit])
result = job.result()
```
</TabItem>
Expand Down Expand Up @@ -627,8 +627,8 @@ another_isa_circuit = pm.run(another_circuit)

with Session(service=service, backend=backend) as session:
sampler = Sampler(session=session)
job = sampler.run([(isa_circuit)])
another_job = sampler.run([(another_isa_circuit)])
job = sampler.run([isa_circuit])
another_job = sampler.run([another_isa_circuit])
result = job.result()
another_result = another_job.result()

Expand Down
2 changes: 1 addition & 1 deletion docs/run/primitives-get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ For Sampler V2, the circuit and optional parameter value sets are input as *prim
<TabItem value="SamplerV2" label="Sampler V2">

```python
job = sampler.run([(isa_circuit)])
job = sampler.run([isa_circuit])
print(f">>> Job ID: {job.job_id()}")
print(f">>> Job Status: {job.status()}")
```
Expand Down
5 changes: 4 additions & 1 deletion docs/run/primitives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ sampler.run([circuit1, circuit2, ...],[observable1, observable2, ...],[param_val
Example:

```python
sampler.run([(circuit1, param_values1, shots1),(circuit2, param_values2, shots_2) ])
sampler.run([
(circuit1, param_values1, shots1),
(circuit2, param_values2, shots_2),
])
```

## Estimator
Expand Down
2 changes: 1 addition & 1 deletion docs/run/quantum-serverless.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ target_circuit = selector.optimized_circuit

# Step 3: Execute the target circuit
sampler = Sampler(backend=backend, options={"default_shots": 1024})
job = sampler.run([(target_circuit,)])
job = sampler.run([target_circuit])
result = job.result()

# Step 4: Postprocess the results
Expand Down
2 changes: 1 addition & 1 deletion docs/run/run-jobs-in-session.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ with Session(backend=backend):
sampler = Sampler()
estimator = Estimator()

job = sampler.run([(sampler_circuit,)])
job = sampler.run([sampler_circuit])
result = job.result()
print(f" > Counts: {result[0].data.meas.get_counts()}")

Expand Down
4 changes: 2 additions & 2 deletions docs/start/setup-channel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ service = QiskitRuntimeService()
backend = service.backend("ibmq_qasm_simulator")

sampler = Sampler(backend)
job = sampler.run([(example_circuit,)])
job = sampler.run([example_circuit])
print(f"job id: {job.job_id()}")
result = job.result()
print(result)
Expand Down Expand Up @@ -173,7 +173,7 @@ print(result)
service = QiskitRuntimeService()
backend = service.backend("ibmq_qasm_simulator")
sampler = Sampler(backend)
job = sampler.run([(example_circuit,)])
job = sampler.run([example_circuit])
print(f"job id: {job.job_id()}")
result = job.result()
print(result)
Expand Down

0 comments on commit 0a0bc66

Please sign in to comment.