forked from roshansingh77/VQF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_quantum_computer.py
1311 lines (1094 loc) · 53 KB
/
_quantum_computer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##############################################################################
# Copyright 2018 Rigetti Computing
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
import itertools
import re
import socket
import subprocess
import warnings
from contextlib import contextmanager
from math import pi, log
from typing import (
Any,
Tuple,
Iterator,
Mapping,
Optional,
Sequence,
Set,
Union,
cast,
List,
)
import httpx
import networkx as nx
import numpy as np
from qcs_api_client.client import QCSClientConfiguration
from qcs_api_client.models import ListQuantumProcessorsResponse
from qcs_api_client.operations.sync import list_quantum_processors
from rpcq.messages import ParameterAref
from pyquil.api import EngagementManager
from pyquil.api._abstract_compiler import AbstractCompiler, QuantumExecutable
from pyquil.api._compiler import QPUCompiler, QVMCompiler
from pyquil.api._qam import QAM, QAMExecutionResult
from pyquil.api._qcs_client import qcs_client
from pyquil.api._qpu import QPU
from pyquil.api._qvm import QVM
from pyquil.experiment._main import Experiment
from pyquil.experiment._memory import merge_memory_map_lists
from pyquil.experiment._result import ExperimentResult, bitstrings_to_expectations
from pyquil.experiment._setting import ExperimentSetting
from pyquil.external.rpcq import CompilerISA
from pyquil.gates import RX, MEASURE
from pyquil.noise import decoherence_noise_with_asymmetric_ro, NoiseModel
from pyquil.paulis import PauliTerm
from pyquil.pyqvm import PyQVM
from pyquil.quantum_processor import (
AbstractQuantumProcessor,
QCSQuantumProcessor,
NxQuantumProcessor,
get_qcs_quantum_processor,
)
from pyquil.quil import Program
import copy
class QuantumComputer:
def __init__(
self,
*,
name: str,
qam: QAM[Any],
compiler: AbstractCompiler,
symmetrize_readout: bool = False,
) -> None:
"""
A quantum computer for running quantum programs.
A quantum computer has various characteristics like supported gates, qubits, qubit
topologies, gate fidelities, and more. A quantum computer also has the ability to
run quantum programs.
A quantum computer can be a real Rigetti QPU that uses superconducting transmon
qubits to run quantum programs, or it can be an emulator like the QVM with
noise models and mimicked topologies.
:param name: A string identifying this particular quantum computer.
:param qam: A quantum abstract machine which handles executing quantum programs. This
dispatches to a QVM or QPU.
:param symmetrize_readout: Whether to apply readout error symmetrization. See
:py:func:`run_symmetrized_readout` for a complete description.
"""
self.name = name
self.qam = qam
self.compiler = compiler
self.symmetrize_readout = symmetrize_readout
@property
def quantum_processor(self) -> AbstractQuantumProcessor:
"""
The quantum processor associated with this quantum computer.
"""
return self.compiler.quantum_processor
def qubits(self) -> List[int]:
"""
Return a sorted list of this QuantumComputer's quantum_processor's qubits
See :py:func:`AbstractQuantumProcessor.qubits` for more.
"""
return self.compiler.quantum_processor.qubits()
def qubit_topology(self) -> nx.graph:
"""
Return a NetworkX graph representation of this QuantumComputer's quantum_processor's qubit
connectivity.
See :py:func:`AbstractQuantumProcessor.qubit_topology` for more.
"""
return self.compiler.quantum_processor.qubit_topology()
def to_compiler_isa(self) -> CompilerISA:
"""
Return a ``CompilerISA`` for this QuantumComputer's quantum_processor.
See :py:func:`AbstractQuantumProcessor.to_compiler_isa` for more.
"""
return self.compiler.quantum_processor.to_compiler_isa()
def run(
self,
executable: QuantumExecutable,
) -> QAMExecutionResult:
"""
Run a quil executable. All parameters in the executable must have values applied using
``Program#write_memory``.
:param executable: The program to run, previously compiled as needed for its target QAM.
:return: execution result including readout data.
"""
return self.qam.run(executable)
def calibrate(self, experiment: Experiment) -> List[ExperimentResult]:
"""
Perform readout calibration on the various multi-qubit observables involved in the provided
``Experiment``.
:param experiment: The ``Experiment`` to calibrate readout error for.
:return: A list of ``ExperimentResult`` objects that contain the expectation values that
correspond to the scale factors resulting from symmetric readout error.
"""
calibration_experiment = experiment.generate_calibration_experiment()
return self.run_experiment(calibration_experiment)
def run_experiment(
self,
experiment: Experiment,
memory_map: Optional[Mapping[str, Sequence[Union[int, float]]]] = None,
) -> List[ExperimentResult]:
"""
Run an ``Experiment`` on a QVM or QPU backend. An ``Experiment`` is composed of:
- A main ``Program`` body (or ansatz).
- A collection of ``ExperimentSetting`` objects, each of which encodes a particular
state preparation and measurement.
- A ``SymmetrizationLevel`` for enacting different readout symmetrization strategies.
- A number of shots to collect for each (unsymmetrized) ``ExperimentSetting``.
Because the main ``Program`` is static from run to run of an ``Experiment``, we can leverage
our platform's Parametric Compilation feature. This means that the ``Program`` can be
compiled only once, and the various alterations due to state preparation, measurement,
and symmetrization can all be realized at runtime by providing a ``memory_map``. Thus, the
steps in the ``experiment`` method are as follows:
1. Generate a parameterized program corresponding to the ``Experiment``
(see the ``Experiment.generate_experiment_program()`` method for more
details on how it changes the main body program to support state preparation,
measurement, and symmetrization).
2. Compile the parameterized program into a parametric (binary) executable, which
contains declared variables that can be assigned at runtime.
3. For each ``ExperimentSetting`` in the ``Experiment``, we repeat the following:
a. Build a collection of memory maps that correspond to the various state
preparation, measurement, and symmetrization specifications.
b. Run the parametric executable on the QVM or QPU backend, providing the memory map
to assign variables at runtime.
c. Extract the desired statistics from the classified bitstrings that are produced
by the QVM or QPU backend, and package them in an ``ExperimentResult`` object.
3. Return the list of ``ExperimentResult`` objects.
This method is extremely useful shorthand for running near-term applications and algorithms,
which often have this ansatz + settings structure.
:param experiment: The ``Experiment`` to run.
:param memory_map: A dictionary mapping declared variables / parameters to their values.
The values are a list of floats or integers. Each float or integer corresponds to
a particular classical memory register. The memory map provided to the ``experiment``
method corresponds to variables in the main body program that we would like to change
at runtime (e.g. the variational parameters provided to the ansatz of the variational
quantum eigensolver).
:return: A list of ``ExperimentResult`` objects containing the statistics gathered
according to the specifications of the ``Experiment``.
"""
experiment_program = experiment.generate_experiment_program()
executable = self.compile(experiment_program)
if memory_map is None:
memory_map = {}
results = []
for settings in experiment:
# TODO: add support for grouped ExperimentSettings
if len(settings) > 1:
raise ValueError("We only support length-1 settings for now.")
setting = settings[0]
qubits = cast(List[int], setting.out_operator.get_qubits())
experiment_setting_memory_map = experiment.build_setting_memory_map(setting)
symmetrization_memory_maps = experiment.build_symmetrization_memory_maps(qubits)
merged_memory_maps = merge_memory_map_lists([experiment_setting_memory_map], symmetrization_memory_maps)
all_bitstrings = []
for merged_memory_map in merged_memory_maps:
final_memory_map = {**memory_map, **merged_memory_map}
executable_copy = executable.copy()
final_memory_map = cast(Mapping[Union[str, ParameterAref], Union[int, float]], final_memory_map)
executable_copy._memory.write(final_memory_map)
bitstrings = self.run(executable_copy).readout_data.get("ro")
assert bitstrings is not None
if "symmetrization" in final_memory_map:
bitmask = np.array(np.array(final_memory_map["symmetrization"]) / np.pi, dtype=int)
bitstrings = np.bitwise_xor(bitstrings, bitmask)
all_bitstrings.append(bitstrings)
symmetrized_bitstrings = np.concatenate(all_bitstrings) # type: ignore
joint_expectations = [experiment.get_meas_registers(qubits)]
if setting.additional_expectations:
joint_expectations += setting.additional_expectations
expectations = bitstrings_to_expectations(symmetrized_bitstrings, joint_expectations=joint_expectations)
means = np.mean(expectations, axis=0)
std_errs = np.std(expectations, axis=0, ddof=1) / np.sqrt(len(expectations))
joint_results = []
for qubit_subset, mean, std_err in zip(joint_expectations, means, std_errs):
out_operator = PauliTerm.from_list([(setting.out_operator[i], i) for i in qubit_subset])
s = ExperimentSetting(
in_state=setting.in_state,
out_operator=out_operator,
additional_expectations=None,
)
r = ExperimentResult(setting=s, expectation=mean, std_err=std_err, total_counts=len(expectations))
joint_results.append(r)
result = ExperimentResult(
setting=setting,
expectation=joint_results[0].expectation,
std_err=joint_results[0].std_err,
total_counts=joint_results[0].total_counts,
additional_results=joint_results[1:],
)
results.append(result)
return results
def run_symmetrized_readout(
self,
program: Program,
trials: int,
symm_type: int = 3,
meas_qubits: Optional[List[int]] = None,
) -> np.ndarray:
r"""
Run a quil program in such a way that the readout error is made symmetric. Enforcing
symmetric readout error is useful in simplifying the assumptions in some near
term error mitigation strategies, see ``measure_observables`` for more information.
The simplest example is for one qubit. In a noisy quantum_processor, the probability of accurately
reading the 0 state might be higher than that of the 1 state; due to e.g. amplitude
damping. This makes correcting for readout more difficult. In the simplest case, this
function runs the program normally ``(trials//2)`` times. The other half of the time,
it will insert an ``X`` gate prior to any ``MEASURE`` instruction and then flip the
measured classical bit back. Overall this has the effect of symmetrizing the readout error.
The details. Consider preparing the input bitstring ``|i>`` (in the computational basis) and
measuring in the Z basis. Then the Confusion matrix for the readout error is specified by
the probabilities
p(j|i) := Pr(measured = j | prepared = i ).
In the case of a single qubit i,j \in [0,1] then:
there is no readout error if p(0|0) = p(1|1) = 1.
the readout error is symmetric if p(0|0) = p(1|1) = 1 - epsilon.
the readout error is asymmetric if p(0|0) != p(1|1).
If your quantum computer has this kind of asymmetric readout error then
``qc.run_symmetrized_readout`` will symmetrize the readout error.
The readout error above is only asymmetric on a single bit. In practice the confusion
matrix on n bits need not be symmetric, e.g. for two qubits p(ij|ij) != 1 - epsilon for
all i,j. In these situations a more sophisticated means of symmetrization is needed; and
we use orthogonal arrays (OA) built from Hadamard matrices.
The symmetrization types are specified by an int; the types available are:
-1 -- exhaustive symmetrization uses every possible combination of flips
0 -- trivial that is no symmetrization
1 -- symmetrization using an OA with strength 1
2 -- symmetrization using an OA with strength 2
3 -- symmetrization using an OA with strength 3
In the context of readout symmetrization the strength of the orthogonal array enforces
the symmetry of the marginal confusion matrices.
By default a strength 3 OA is used; this ensures expectations of the form
``<b_k . b_j . b_i>`` for bits any bits i,j,k will have symmetric readout errors. Here
expectation of a random variable x as is denote ``<x> = sum_i Pr(i) x_i``. It turns out that
a strength 3 OA is also a strength 2 and strength 1 OA it also ensures ``<b_j . b_i>`` and
``<b_i>`` have symmetric readout errors for any bits b_j and b_i.
:param program: The program to run symmetrized readout on.
:param trials: The minimum number of times to run the program; it is recommend that this
number should be in the hundreds or thousands. This parameter will be mutated if
necessary.
:param symm_type: the type of symmetrization
:param meas_qubits: An advanced feature. The groups of measurement qubits. Only these
qubits will be symmetrized over, even if the program acts on other qubits.
:return: A numpy array of shape (trials, len(ro-register)) that contains 0s and 1s.
"""
if not isinstance(symm_type, int):
raise ValueError(
"Symmetrization options are indicated by an int. See " "the docstrings for more information."
)
if meas_qubits is None:
meas_qubits = list(cast(Set[int], program.get_qubits()))
# It is desirable to have hundreds or thousands of trials more than the minimum
trials = _check_min_num_trials_for_symmetrized_readout(len(meas_qubits), trials, symm_type)
sym_programs, flip_arrays = _symmetrization(program, meas_qubits, symm_type)
# Floor division so e.g. 9 // 8 = 1 and 17 // 8 = 2.
num_shots_per_prog = trials // len(sym_programs)
if num_shots_per_prog * len(sym_programs) < trials:
warnings.warn(
f"The number of trials was modified from {trials} to "
f"{num_shots_per_prog * len(sym_programs)}. To be consistent with the "
f"number of trials required by the type of readout symmetrization "
f"chosen."
)
results = _measure_bitstrings(self, sym_programs, meas_qubits, num_shots_per_prog)
return _consolidate_symmetrization_outputs(results, flip_arrays)
def compile(
self,
program: Program,
to_native_gates: bool = True,
optimize: bool = True,
*,
protoquil: Optional[bool] = None,
) -> QuantumExecutable:
"""
A high-level interface to program compilation.
Compilation currently consists of two stages. Please see the :py:class:`AbstractCompiler`
docs for more information. This function does all stages of compilation.
Right now both ``to_native_gates`` and ``optimize`` must be either both set or both
unset. More modular compilation passes may be available in the future.
Additionally, a call to compile also calls the ``reset`` method if one is running
on the QPU. This is a bit of a sneaky hack to guard against stale compiler connections,
but shouldn't result in any material hit to performance (especially when taking advantage
of parametric compilation for hybrid applications).
:param program: A Program
:param to_native_gates: Whether to compile non-native gates to native gates.
:param optimize: Whether to optimize the program to reduce the number of operations.
:param protoquil: Whether to restrict the input program to and the compiled program
to protoquil (executable on QPU). A value of ``None`` means defer to server.
:return: An executable binary suitable for passing to :py:func:`QuantumComputer.run`.
"""
flags = [to_native_gates, optimize]
assert all(flags) or all(not f for f in flags), "Must turn quilc all on or all off"
quilc = all(flags)
if quilc:
nq_program = self.compiler.quil_to_native_quil(program, protoquil=protoquil)
else:
nq_program = program
return self.compiler.native_quil_to_executable(nq_program)
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return f'QuantumComputer[name="{self.name}"]'
def list_quantum_computers(
qpus: bool = True,
qvms: bool = True,
timeout: float = 10.0,
client_configuration: Optional[QCSClientConfiguration] = None,
) -> List[str]:
"""
List the names of available quantum computers
:param qpus: Whether to include QPUs in the list.
:param qvms: Whether to include QVMs in the list.
:param timeout: Time limit for request, in seconds.
:param client_configuration: Optional client configuration. If none is provided, a default one will be loaded.
"""
client_configuration = client_configuration or QCSClientConfiguration.load()
qc_names: List[str] = []
if qpus:
with qcs_client(
client_configuration=client_configuration, request_timeout=timeout
) as client: # type: httpx.Client
qcs: ListQuantumProcessorsResponse = list_quantum_processors(client=client, page_size=100).parsed
qc_names += [qc.id for qc in qcs.quantum_processors]
if qvms:
qc_names += ["9q-square-qvm", "9q-square-noisy-qvm"]
return qc_names
def _parse_name(name: str, as_qvm: Optional[bool], noisy: Optional[bool]) -> Tuple[str, Optional[str], bool]:
"""
Try to figure out whether we're getting a (noisy) qvm, and the associated qpu name.
See :py:func:`get_qc` for examples of valid names + flags.
"""
qvm_type: Optional[str]
parts = name.split("-")
if len(parts) >= 2 and parts[-2] == "noisy" and parts[-1] in ["qvm", "pyqvm"]:
if as_qvm is not None and (not as_qvm):
raise ValueError(
"The provided qc name indicates you are getting a noisy QVM, " "but you have specified `as_qvm=False`"
)
if noisy is not None and (not noisy):
raise ValueError(
"The provided qc name indicates you are getting a noisy QVM, " "but you have specified `noisy=False`"
)
qvm_type = parts[-1]
noisy = True
prefix = "-".join(parts[:-2])
return prefix, qvm_type, noisy
if len(parts) >= 1 and parts[-1] in ["qvm", "pyqvm"]:
if as_qvm is not None and (not as_qvm):
raise ValueError(
"The provided qc name indicates you are getting a QVM, " "but you have specified `as_qvm=False`"
)
qvm_type = parts[-1]
if noisy is None:
noisy = False
prefix = "-".join(parts[:-1])
return prefix, qvm_type, noisy
if as_qvm is not None and as_qvm:
qvm_type = "qvm"
else:
qvm_type = None
if noisy is None:
noisy = False
return name, qvm_type, noisy
def _canonicalize_name(prefix: str, qvm_type: Optional[str], noisy: bool) -> str:
"""Take the output of _parse_name to create a canonical name."""
if noisy:
noise_suffix = "-noisy"
else:
noise_suffix = ""
if qvm_type is None:
qvm_suffix = ""
elif qvm_type == "qvm":
qvm_suffix = "-qvm"
elif qvm_type == "pyqvm":
qvm_suffix = "-pyqvm"
else:
raise ValueError(f"Unknown qvm_type {qvm_type}")
name = f"{prefix}{noise_suffix}{qvm_suffix}"
return name
def _get_qvm_or_pyqvm(
*,
client_configuration: QCSClientConfiguration,
qvm_type: str,
noise_model: Optional[NoiseModel],
quantum_processor: Optional[AbstractQuantumProcessor],
execution_timeout: float,
) -> Union[QVM, PyQVM]:
if qvm_type == "qvm":
return QVM(noise_model=noise_model, timeout=execution_timeout, client_configuration=client_configuration)
elif qvm_type == "pyqvm":
assert quantum_processor is not None
return PyQVM(n_qubits=quantum_processor.qubit_topology().number_of_nodes())
raise ValueError("Unknown qvm type {}".format(qvm_type))
def _get_qvm_qc(
*,
client_configuration: QCSClientConfiguration,
name: str,
qvm_type: str,
quantum_processor: AbstractQuantumProcessor,
compiler_timeout: float,
execution_timeout: float,
noise_model: Optional[NoiseModel],
) -> QuantumComputer:
"""Construct a QuantumComputer backed by a QVM.
This is a minimal wrapper over the QuantumComputer, QVM, and QVMCompiler constructors.
:param client_configuration: Client configuration.
:param name: A string identifying this particular quantum computer.
:param qvm_type: The type of QVM. Either qvm or pyqvm.
:param quantum_processor: A quantum_processor following the AbstractQuantumProcessor interface.
:param noise_model: An optional noise model
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:return: A QuantumComputer backed by a QVM with the above options.
"""
return QuantumComputer(
name=name,
qam=_get_qvm_or_pyqvm(
client_configuration=client_configuration,
qvm_type=qvm_type,
noise_model=noise_model,
quantum_processor=quantum_processor,
execution_timeout=execution_timeout,
),
compiler=QVMCompiler(
quantum_processor=quantum_processor,
timeout=compiler_timeout,
client_configuration=client_configuration,
),
)
def _get_qvm_with_topology(
*,
client_configuration: QCSClientConfiguration,
name: str,
topology: nx.Graph,
noisy: bool,
qvm_type: str,
compiler_timeout: float,
execution_timeout: float,
) -> QuantumComputer:
"""Construct a QVM with the provided topology.
:param client_configuration: Client configuration.
:param name: A name for your quantum computer. This field does not affect behavior of the
constructed QuantumComputer.
:param topology: A graph representing the desired qubit connectivity.
:param noisy: Whether to include a generic noise model. If you want more control over
the noise model, please construct your own :py:class:`NoiseModel` and use
:py:func:`_get_qvm_qc` instead of this function.
:param qvm_type: The type of QVM. Either 'qvm' or 'pyqvm'.
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:return: A pre-configured QuantumComputer
"""
# Note to developers: consider making this function public and advertising it.
quantum_processor = NxQuantumProcessor(topology=topology)
if noisy:
noise_model: Optional[NoiseModel] = decoherence_noise_with_asymmetric_ro(
isa=quantum_processor.to_compiler_isa()
)
else:
noise_model = None
return _get_qvm_qc(
client_configuration=client_configuration,
name=name,
qvm_type=qvm_type,
quantum_processor=quantum_processor,
noise_model=noise_model,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
def _get_9q_square_qvm(
*,
client_configuration: QCSClientConfiguration,
name: str,
noisy: bool,
qvm_type: str,
compiler_timeout: float,
execution_timeout: float,
) -> QuantumComputer:
"""
A nine-qubit 3x3 square lattice.
This uses a "generic" lattice not tied to any specific quantum_processor. 9 qubits is large enough
to do vaguely interesting algorithms and small enough to simulate quickly.
:param client_configuration: Client configuration.
:param name: The name of this QVM
:param noisy: Whether to construct a noisy quantum computer
:param qvm_type: The type of QVM. Either 'qvm' or 'pyqvm'.
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:return: A pre-configured QuantumComputer
"""
topology = nx.convert_node_labels_to_integers(nx.grid_2d_graph(3, 3))
return _get_qvm_with_topology(
client_configuration=client_configuration,
name=name,
topology=topology,
noisy=noisy,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
def _get_unrestricted_qvm(
*,
client_configuration: QCSClientConfiguration,
name: str,
noisy: bool,
n_qubits: int,
qvm_type: str,
compiler_timeout: float,
execution_timeout: float,
) -> QuantumComputer:
"""
A qvm with a fully-connected topology.
This is obviously the least realistic QVM, but who am I to tell users what they want.
:param client_configuration: Client configuration.
:param name: The name of this QVM
:param noisy: Whether to construct a noisy quantum computer
:param n_qubits: 34 qubits ought to be enough for anybody.
:param qvm_type: The type of QVM. Either 'qvm' or 'pyqvm'.
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:return: A pre-configured QuantumComputer
"""
topology = nx.complete_graph(n_qubits)
return _get_qvm_with_topology(
client_configuration=client_configuration,
name=name,
topology=topology,
noisy=noisy,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
def _get_qvm_based_on_real_quantum_processor(
*,
client_configuration: QCSClientConfiguration,
name: str,
quantum_processor: QCSQuantumProcessor,
noisy: bool,
qvm_type: str,
compiler_timeout: float,
execution_timeout: float,
) -> QuantumComputer:
"""
A qvm with a based on a real quantum_processor.
This is the most realistic QVM.
:param client_configuration: Client configuration.
:param name: The full name of this QVM
:param quantum_processor: The quantum_processor from :py:func:`get_lattice`.
:param noisy: Whether to construct a noisy quantum computer by using the quantum_processor's
associated noise model.
:param qvm_type: The type of QVM. Either 'qvm' or 'pyqvm'.
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:return: A pre-configured QuantumComputer based on the named quantum_processor.
"""
if noisy:
noise_model = quantum_processor.noise_model
else:
noise_model = None
return _get_qvm_qc(
client_configuration=client_configuration,
name=name,
quantum_processor=quantum_processor,
noise_model=noise_model,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
def get_qc(
name: str,
*,
as_qvm: Optional[bool] = None,
noisy: Optional[bool] = None,
compiler_timeout: float = 10.0,
execution_timeout: float = 10.0,
client_configuration: Optional[QCSClientConfiguration] = None,
endpoint_id: Optional[str] = None,
engagement_manager: Optional[EngagementManager] = None,
) -> QuantumComputer:
"""
Get a quantum computer.
A quantum computer is an object of type :py:class:`QuantumComputer` and can be backed
either by a QVM simulator ("Quantum/Quil Virtual Machine") or a physical Rigetti QPU ("Quantum
Processing Unit") made of superconducting qubits.
You can choose the quantum computer to target through a combination of its name and optional
flags. There are multiple ways to get the same quantum computer. The following are equivalent::
>>> qc = get_qc("Aspen-1-16Q-A-noisy-qvm")
>>> qc = get_qc("Aspen-1-16Q-A", as_qvm=True, noisy=True)
and will construct a simulator of an Aspen-1 lattice with a noise model based on quantum_processor
characteristics. We also provide a means for constructing generic quantum simulators that
are not related to a given piece of Rigetti hardware::
>>> qc = get_qc("9q-square-qvm")
>>> qc = get_qc("9q-square", as_qvm=True)
Finally, you can get request a QVM with "no" topology of a given number of qubits
(technically, it's a fully connected graph among the given number of qubits) with::
>>> qc = get_qc("5q-qvm") # or "6q-qvm", or "34q-qvm", ...
These less-realistic, fully-connected QVMs will also be more lenient on what types of programs
they will ``run``. Specifically, you do not need to do any compilation. For the other, realistic
QVMs you must use :py:func:`qc.compile` or :py:func:`qc.compiler.native_quil_to_executable`
prior to :py:func:`qc.run`.
The Rigetti QVM must be downloaded from https://www.rigetti.com/forest and run as a server
alongside your python program. To use pyQuil's built-in QVM, replace all ``"-qvm"`` suffixes
with ``"-pyqvm"``::
>>> qc = get_qc("5q-pyqvm")
Redundant flags are acceptable, but conflicting flags will raise an exception::
>>> qc = get_qc("9q-square-qvm") # qc is fully specified by its name
>>> qc = get_qc("9q-square-qvm", as_qvm=True) # redundant, but ok
>>> qc = get_qc("9q-square-qvm", as_qvm=False) # Error!
Use :py:func:`list_quantum_computers` to retrieve a list of known qc names.
This method is provided as a convenience to quickly construct and use QVM's and QPU's.
Power users may wish to have more control over the specification of a quantum computer
(e.g. custom noise models, bespoke topologies, etc.). This is possible by constructing
a :py:class:`QuantumComputer` object by hand. Please refer to the documentation on
:py:class:`QuantumComputer` for more information.
:param name: The name of the desired quantum computer. This should correspond to a name
returned by :py:func:`list_quantum_computers`. Names ending in "-qvm" will return
a QVM. Names ending in "-pyqvm" will return a :py:class:`PyQVM`. Names ending in
"-noisy-qvm" will return a QVM with a noise model. Otherwise, we will return a QPU with
the given name.
:param as_qvm: An optional flag to force construction of a QVM (instead of a QPU). If
specified and set to ``True``, a QVM-backed quantum computer will be returned regardless
of the name's suffix
:param noisy: An optional flag to force inclusion of a noise model. If
specified and set to ``True``, a quantum computer with a noise model will be returned
regardless of the name's suffix. The generic QVM noise model is simple T1 and T2 noise
plus readout error. See :py:func:`~pyquil.noise.decoherence_noise_with_asymmetric_ro`.
Note, we currently do not support noise models based on QCS hardware; a value of
`True`` will result in an error if the requested QPU is a QCS hardware QPU.
:param compiler_timeout: Time limit for compilation requests, in seconds.
:param execution_timeout: Time limit for execution requests, in seconds.
:param client_configuration: Optional client configuration. If none is provided, a default one will be loaded.
For more information on setting up QCS credentials, see documentation for using the QCS CLI:
[https://docs.rigetti.com/qcs/guides/using-the-qcs-cli#configuring-credentials].
:param endpoint_id: Optional quantum processor endpoint ID, as used in the `QCS API Docs`_.
:param engagement_manager: Optional engagement manager. If none is provided, a default one will be created.
:return: A pre-configured QuantumComputer
.. _QCS API Docs: https://docs.api.qcs.rigetti.com/#tag/endpoints
"""
client_configuration = client_configuration or QCSClientConfiguration.load()
engagement_manager = engagement_manager or EngagementManager(client_configuration=client_configuration)
# 1. Parse name, check for redundant options, canonicalize names.
prefix, qvm_type, noisy = _parse_name(name, as_qvm, noisy)
del as_qvm # do not use after _parse_name
name = _canonicalize_name(prefix, qvm_type, noisy)
# 2. Check for unrestricted {n}q-qvm
ma = re.fullmatch(r"(\d+)q", prefix)
if ma is not None:
n_qubits = int(ma.group(1))
if qvm_type is None:
raise ValueError("Please name a valid quantum_processor or run as a QVM")
return _get_unrestricted_qvm(
client_configuration=client_configuration,
name=name,
noisy=noisy,
n_qubits=n_qubits,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
# 3. Check for "9q-square" qvm
if prefix == "9q-square":
if qvm_type is None:
raise ValueError("The quantum_processor '9q-square' is only available as a QVM")
return _get_9q_square_qvm(
client_configuration=client_configuration,
name=name,
noisy=noisy,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
if noisy:
raise ValueError(
"pyQuil currently does not support initializing a noisy QuantumComputer "
"based on a QCSQuantumProcessor. Change noisy to False or specify the name of a QVM."
)
# 4. Not a special case, query the web for information about this quantum_processor.
quantum_processor = get_qcs_quantum_processor(
quantum_processor_id=prefix, client_configuration=client_configuration
)
if qvm_type is not None:
# 4.1 QVM based on a real quantum_processor.
return _get_qvm_based_on_real_quantum_processor(
client_configuration=client_configuration,
name=name,
quantum_processor=quantum_processor,
noisy=False,
qvm_type=qvm_type,
compiler_timeout=compiler_timeout,
execution_timeout=execution_timeout,
)
else:
qpu = QPU(
quantum_processor_id=quantum_processor.quantum_processor_id,
timeout=execution_timeout,
client_configuration=client_configuration,
endpoint_id=endpoint_id,
engagement_manager=engagement_manager,
)
compiler = QPUCompiler(
quantum_processor_id=prefix,
quantum_processor=quantum_processor,
timeout=compiler_timeout,
client_configuration=client_configuration,
)
return QuantumComputer(name=name, qam=qpu, compiler=compiler)
def _port_used(host: str, port: int) -> bool:
"""Check if a (TCP) port is listening.
:param host: Host address to check.
:param port: TCP port to check.
:returns: ``True`` if a process is listening on the specified host/port, ``False`` otherwise
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
return True
except ConnectionRefusedError:
return False
finally:
s.close()
@contextmanager
def local_forest_runtime(
*,
host: str = "127.0.0.1",
qvm_port: int = 5000,
quilc_port: int = 5555,
use_protoquil: bool = False,
) -> Iterator[Tuple[Optional[subprocess.Popen], Optional[subprocess.Popen]]]:
"""A context manager for local QVM and QUIL compiler.
You must first have installed the `qvm` and `quilc` executables from
the forest SDK. [https://www.rigetti.com/forest]
This context manager will ensure that the designated ports are not used, start up `qvm` and
`quilc` proccesses if possible and terminate them when the context is exited.
If one of the ports is in use, a ``RuntimeWarning`` will be issued and the `qvm`/`quilc` process
won't be started.
.. note::
Only processes started by this context manager will be terminated on exit, no external
process will be touched.
>>> from pyquil import get_qc, Program
>>> from pyquil.gates import CNOT, Z
>>> from pyquil.api import local_forest_runtime
>>>
>>> qvm = get_qc('9q-square-qvm')
>>> prog = Program(Z(0), CNOT(0, 1))
>>>
>>> with local_forest_runtime():
>>> results = qvm.run_and_measure(prog, trials=10)
:param host: Host on which `qvm` and `quilc` should listen on.
:param qvm_port: Port which should be used by `qvm`.
:param quilc_port: Port which should be used by `quilc`.
:param use_protoquil: Restrict input/output to protoquil.
.. warning::
If ``use_protoquil`` is set to ``True`` language features you need
may be disabled. Please use it with caution.
:raises: FileNotFoundError: If either executable is not installed.
:returns: The returned tuple contains two ``subprocess.Popen`` objects
for the `qvm` and the `quilc` processes. If one of the designated
ports is in use, the process won't be started and the respective
value in the tuple will be ``None``.
"""
qvm: Optional[subprocess.Popen] = None
quilc: Optional[subprocess.Popen] = None
# If the host we should listen to is 0.0.0.0, we replace it
# with 127.0.0.1 to use a valid IP when checking if the port is in use.
if _port_used(host if host != "0.0.0.0" else "127.0.0.1", qvm_port):
warning_msg = ("Unable to start qvm server, since the specified port {} is in use.").format(qvm_port)
warnings.warn(RuntimeWarning(warning_msg))
else:
qvm_cmd = ["qvm", "-S", "--host", host, "-p", str(qvm_port)]
qvm = subprocess.Popen(qvm_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if _port_used(host if host != "0.0.0.0" else "127.0.0.1", quilc_port):
warning_msg = ("Unable to start quilc server, since the specified port {} is in use.").format(quilc_port)
warnings.warn(RuntimeWarning(warning_msg))
else:
quilc_cmd = ["quilc", "--host", host, "-p", str(quilc_port), "-R"]
if use_protoquil:
quilc_cmd += ["-P"]
quilc = subprocess.Popen(quilc_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Return context
try:
yield (qvm, quilc)
finally:
# Exit. Release resource
if qvm:
qvm.terminate()
if quilc:
quilc.terminate()
def _flip_array_to_prog(flip_array: Tuple[bool], qubits: List[int]) -> Program:
"""
Generate a pre-measurement program that flips the qubit state according to the flip_array of
bools.
This is used, for example, in symmetrization to produce programs which flip a select subset
of qubits immediately before measurement.
:param flip_array: tuple of booleans specifying whether the qubit in the corresponding index
should be flipped or not.
:param qubits: list specifying the qubits in order corresponding to the flip_array
:return: Program which flips each qubit (i.e. instructs RX(pi, q)) according to the flip_array.
"""