Skip to content

Commit 6d63cf6

Browse files
feat: execute several circuits with a single call to "run"
1 parent dd27aa8 commit 6d63cf6

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

mpqp/execution/result.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class Result:
195195
Examples:
196196
>>> job = Job(JobType.STATE_VECTOR, QCircuit(2), ATOSDevice.MYQLM_CLINALG)
197197
>>> print(Result(job, StateVector(np.array([1, 1, 1, -1], dtype=np.complex64) / 2, 2), 0, 0)) # doctest: +NORMALIZE_WHITESPACE
198-
Result: ATOSDevice, MYQLM_CLINALG
198+
Result: None, ATOSDevice, MYQLM_CLINALG
199199
State vector: [0.5, 0.5, 0.5, -0.5]
200200
Probabilities: [0.25, 0.25, 0.25, 0.25]
201201
Number of qubits: 2
@@ -204,7 +204,7 @@ class Result:
204204
... Sample(2, index=0, count=250),
205205
... Sample(2, index=3, count=250)
206206
... ], 0.034, 500)) # doctest: +NORMALIZE_WHITESPACE
207-
Result: ATOSDevice, MYQLM_CLINALG
207+
Result: None, ATOSDevice, MYQLM_CLINALG
208208
Counts: [250, 0, 0, 250]
209209
Probabilities: [0.5, 0, 0, 0.5]
210210
Samples:
@@ -213,7 +213,7 @@ class Result:
213213
Error: 0.034
214214
>>> job = Job(JobType.OBSERVABLE, QCircuit(2), ATOSDevice.MYQLM_CLINALG)
215215
>>> print(Result(job, -3.09834, 0.021, 2048)) # doctest: +NORMALIZE_WHITESPACE
216-
Result: ATOSDevice, MYQLM_CLINALG
216+
Result: None, ATOSDevice, MYQLM_CLINALG
217217
Expectation value: -3.09834
218218
Error/Variance: 0.021
219219
@@ -373,7 +373,9 @@ def counts(self) -> list[int]:
373373
return self._counts
374374

375375
def __str__(self):
376-
header = f"Result: {type(self.device).__name__}, {self.device.name}"
376+
header = (
377+
f"Result: {self.job.circuit.label}, {type(self.device).__name__}, {self.device.name}"
378+
)
377379

378380
if self.job.job_type == JobType.SAMPLE:
379381
samples_str = "\n".join(map(lambda s: f" {s}", self.samples))
@@ -483,22 +485,22 @@ class BatchResult:
483485
>>> batch_result = BatchResult([result1, result2, result3])
484486
>>> print(batch_result)
485487
BatchResult: 3 results
486-
Result: ATOSDevice, MYQLM_PYLINALG
488+
Result: None, ATOSDevice, MYQLM_PYLINALG
487489
State vector: [0.5, 0.5, 0.5, -0.5]
488490
Probabilities: [0.25, 0.25, 0.25, 0.25]
489491
Number of qubits: 2
490-
Result: ATOSDevice, MYQLM_PYLINALG
492+
Result: None, ATOSDevice, MYQLM_PYLINALG
491493
Counts: [250, 0, 0, 250]
492494
Probabilities: [0.5, 0, 0, 0.5]
493495
Samples:
494496
State: 00, Index: 0, Count: 250, Probability: 0.5
495497
State: 11, Index: 3, Count: 250, Probability: 0.5
496498
Error: 0.034
497-
Result: ATOSDevice, MYQLM_PYLINALG
499+
Result: None, ATOSDevice, MYQLM_PYLINALG
498500
Expectation value: -3.09834
499501
Error/Variance: 0.021
500502
>>> print(batch_result[0])
501-
Result: ATOSDevice, MYQLM_PYLINALG
503+
Result: None, ATOSDevice, MYQLM_PYLINALG
502504
State vector: [0.5, 0.5, 0.5, -0.5]
503505
Probabilities: [0.25, 0.25, 0.25, 0.25]
504506
Number of qubits: 2

mpqp/execution/runner.py

+47-27
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def _run_single(
199199

200200
@typechecked
201201
def run(
202-
circuit: QCircuit,
202+
circuit: OneOrMany[QCircuit],
203203
device: OneOrMany[AvailableDevice],
204204
values: Optional[dict[Expr | str, Complex]] = None,
205205
) -> Union[Result, BatchResult]:
@@ -220,49 +220,69 @@ def run(
220220
221221
Examples:
222222
>>> c = QCircuit(
223-
... [H(0), CNOT(0, 1), BasisMeasure([0, 1], shots=1000)],
224-
... label="Bell pair",
223+
... [X(0), CNOT(0, 1), BasisMeasure([0, 1], shots=1000)],
224+
... label="X CNOT circuit",
225225
... )
226226
>>> result = run(c, IBMDevice.AER_SIMULATOR)
227-
>>> print(result) # doctest: +SKIP
228-
Result: IBMDevice, AER_SIMULATOR
229-
Counts: [497, 0, 0, 503]
230-
Probabilities: [0.497, 0, 0, 0.503]
227+
>>> print(result)
228+
Result: X CNOT circuit, IBMDevice, AER_SIMULATOR
229+
Counts: [0, 0, 0, 1000]
230+
Probabilities: [0, 0, 0, 1]
231231
Samples:
232-
State: 00, Index: 0, Count: 497, Probability: 0.497
233-
State: 11, Index: 3, Count: 503, Probability: 0.503
232+
State: 11, Index: 3, Count: 1000, Probability: 1.0
234233
Error: None
235234
>>> batch_result = run(
236235
... c,
237236
... [ATOSDevice.MYQLM_PYLINALG, AWSDevice.BRAKET_LOCAL_SIMULATOR]
238237
... )
239-
>>> print(batch_result) # doctest: +SKIP
238+
>>> print(batch_result)
239+
BatchResult: 2 results
240+
Result: X CNOT circuit, AWSDevice, BRAKET_LOCAL_SIMULATOR
241+
Counts: [0, 0, 0, 1000]
242+
Probabilities: [0, 0, 0, 1]
243+
Samples:
244+
State: 11, Index: 3, Count: 1000, Probability: 1.0
245+
Error: None
246+
Result: X CNOT circuit, ATOSDevice, MYQLM_PYLINALG
247+
Counts: [0, 0, 0, 1000]
248+
Probabilities: [0, 0, 0, 1]
249+
Samples:
250+
State: 11, Index: 3, Count: 1000, Probability: 1.0
251+
Error: 0.0
252+
>>> c2 = QCircuit(
253+
... [X(0), X(1), BasisMeasure([0, 1], shots=1000)],
254+
... label="X circuit",
255+
... )
256+
>>> result = run([c,c2], IBMDevice.AER_SIMULATOR)
257+
>>> print(result)
240258
BatchResult: 2 results
241-
Result: ATOSDevice, MYQLM_PYLINALG
242-
Counts: [499, 0, 0, 501]
243-
Probabilities: [0.499, 0, 0, 0.501]
259+
Result: X CNOT circuit, IBMDevice, AER_SIMULATOR
260+
Counts: [0, 0, 0, 1000]
261+
Probabilities: [0, 0, 0, 1]
244262
Samples:
245-
State: 00, Index: 0, Count: 499, Probability: 0.499
246-
State: 11, Index: 3, Count: 501, Probability: 0.501
247-
Error: 0.01581926829057682
248-
Result: AWSDevice, BRAKET_LOCAL_SIMULATOR
249-
Counts: [502, 0, 0, 498]
250-
Probabilities: [0.502, 0, 0, 0.498]
263+
State: 11, Index: 3, Count: 1000, Probability: 1.0
264+
Error: None
265+
Result: X circuit, IBMDevice, AER_SIMULATOR
266+
Counts: [0, 0, 0, 1000]
267+
Probabilities: [0, 0, 0, 1]
251268
Samples:
252-
State: 00, Index: 0, Count: 502, Probability: 0.502
253-
State: 11, Index: 3, Count: 498, Probability: 0.498
269+
State: 11, Index: 3, Count: 1000, Probability: 1.0
254270
Error: None
255271
256272
"""
257-
258273
if values is None:
259274
values = {}
260275

261-
if isinstance(device, Iterable):
262-
return BatchResult([_run_single(circuit, dev, values) for dev in set(device)])
263-
264-
return _run_single(circuit, device, values)
265-
276+
if isinstance(circuit, Iterable):
277+
if isinstance(device, Iterable):
278+
return BatchResult([_run_single(circ, device, values) for circ in circuit for device in set(device)])
279+
else:
280+
return BatchResult([_run_single(circ, device, values) for circ in circuit])
281+
else:
282+
if isinstance(device, Iterable):
283+
return BatchResult([_run_single(circuit, device, values) for device in set(device)])
284+
else:
285+
return _run_single(circuit, device, values)
266286

267287
@typechecked
268288
def submit(

0 commit comments

Comments
 (0)