From 0a0bc66dc32c5041045aca0c7cc3faff0284c90d Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Thu, 4 Apr 2024 09:05:35 -0400 Subject: [PATCH] Use simpler argument style for V2 Sampler (#1122) `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. --- docs/api/migration-guides/v2-primitives.mdx | 19 +++++++++++-------- docs/build/bit-ordering.mdx | 2 +- docs/run/advanced-runtime-options.mdx | 4 ++-- docs/run/estimate-job-run-time.mdx | 2 +- docs/run/primitives-examples.mdx | 6 +++--- docs/run/primitives-get-started.mdx | 2 +- docs/run/primitives.mdx | 5 ++++- docs/run/quantum-serverless.mdx | 2 +- docs/run/run-jobs-in-session.mdx | 2 +- docs/start/setup-channel.mdx | 4 ++-- 10 files changed, 27 insertions(+), 21 deletions(-) diff --git a/docs/api/migration-guides/v2-primitives.mdx b/docs/api/migration-guides/v2-primitives.mdx index a7ecc6db6c9..d9f6ab08c0f 100644 --- a/docs/api/migration-guides/v2-primitives.mdx +++ b/docs/api/migration-guides/v2-primitives.mdx @@ -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 @@ -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] @@ -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() ``` @@ -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() diff --git a/docs/build/bit-ordering.mdx b/docs/build/bit-ordering.mdx index 560ed692774..a355e37ecc4 100644 --- a/docs/build/bit-ordering.mdx +++ b/docs/build/bit-ordering.mdx @@ -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()}") ``` diff --git a/docs/run/advanced-runtime-options.mdx b/docs/run/advanced-runtime-options.mdx index f3a93cf135a..bd764df4455 100644 --- a/docs/run/advanced-runtime-options.mdx +++ b/docs/run/advanced-runtime-options.mdx @@ -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) ``` @@ -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) ``` diff --git a/docs/run/estimate-job-run-time.mdx b/docs/run/estimate-job-run-time.mdx index cfcd511dc96..9a91c4fcbd9 100644 --- a/docs/run/estimate-job-run-time.mdx +++ b/docs/run/estimate-job-run-time.mdx @@ -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) ``` diff --git a/docs/run/primitives-examples.mdx b/docs/run/primitives-examples.mdx index 899b3c0d0a3..223b696a479 100644 --- a/docs/run/primitives-examples.mdx +++ b/docs/run/primitives-examples.mdx @@ -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() ``` @@ -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() diff --git a/docs/run/primitives-get-started.mdx b/docs/run/primitives-get-started.mdx index 7ea93c317ca..32e9f6a51de 100644 --- a/docs/run/primitives-get-started.mdx +++ b/docs/run/primitives-get-started.mdx @@ -205,7 +205,7 @@ For Sampler V2, the circuit and optional parameter value sets are input as *prim ```python -job = sampler.run([(isa_circuit)]) +job = sampler.run([isa_circuit]) print(f">>> Job ID: {job.job_id()}") print(f">>> Job Status: {job.status()}") ``` diff --git a/docs/run/primitives.mdx b/docs/run/primitives.mdx index f7ddd45abf1..13681f636fa 100644 --- a/docs/run/primitives.mdx +++ b/docs/run/primitives.mdx @@ -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 diff --git a/docs/run/quantum-serverless.mdx b/docs/run/quantum-serverless.mdx index b2fad31537d..d5c25604aca 100644 --- a/docs/run/quantum-serverless.mdx +++ b/docs/run/quantum-serverless.mdx @@ -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 diff --git a/docs/run/run-jobs-in-session.mdx b/docs/run/run-jobs-in-session.mdx index 775b98be4bc..c84f5e65c24 100644 --- a/docs/run/run-jobs-in-session.mdx +++ b/docs/run/run-jobs-in-session.mdx @@ -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()}") diff --git a/docs/start/setup-channel.mdx b/docs/start/setup-channel.mdx index 60126855b59..1bae4663663 100644 --- a/docs/start/setup-channel.mdx +++ b/docs/start/setup-channel.mdx @@ -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) @@ -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)