forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhigh_level_synthesis.py
935 lines (740 loc) · 38.2 KB
/
high_level_synthesis.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
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
High Level Synthesis Plugins
-----------------------------
Clifford Synthesis
''''''''''''''''''
.. list-table:: Plugins for :class:`qiskit.quantum_info.Clifford` (key = ``"clifford"``)
:header-rows: 1
* - Plugin name
- Plugin class
- Targeted connectivity
- Description
* - ``"ag"``
- :class:`~.AGSynthesisClifford`
- all-to-all
- greedily optimizes CX-count
* - ``"bm"``
- :class:`~.BMSynthesisClifford`
- all-to-all
- optimal count for `n=2,3`; used in ``"default"`` for `n=2,3`
* - ``"greedy"``
- :class:`~.GreedySynthesisClifford`
- all-to-all
- greedily optimizes CX-count; used in ``"default"`` for `n>=4`
* - ``"layers"``
- :class:`~.LayerSynthesisClifford`
- all-to-all
-
* - ``"lnn"``
- :class:`~.LayerLnnSynthesisClifford`
- linear
- many CX-gates but guarantees CX-depth of at most `7*n+2`
* - ``"default"``
- :class:`~.DefaultSynthesisClifford`
- all-to-all
- usually best for optimizing CX-count (and optimal CX-count for `n=2,3`)
.. autosummary::
:toctree: ../stubs/
AGSynthesisClifford
BMSynthesisClifford
GreedySynthesisClifford
LayerSynthesisClifford
LayerLnnSynthesisClifford
DefaultSynthesisClifford
Linear Function Synthesis
'''''''''''''''''''''''''
.. list-table:: Plugins for :class:`.LinearFunction` (key = ``"linear"``)
:header-rows: 1
* - Plugin name
- Plugin class
- Targeted connectivity
- Description
* - ``"kms"``
- :class:`~.KMSSynthesisLinearFunction`
- linear
- many CX-gates but guarantees CX-depth of at most `5*n`
* - ``"pmh"``
- :class:`~.PMHSynthesisLinearFunction`
- all-to-all
- greedily optimizes CX-count; used in ``"default"``
* - ``"default"``
- :class:`~.DefaultSynthesisLinearFunction`
- all-to-all
- best for optimizing CX-count
.. autosummary::
:toctree: ../stubs/
KMSSynthesisLinearFunction
PMHSynthesisLinearFunction
DefaultSynthesisLinearFunction
Permutation Synthesis
'''''''''''''''''''''
.. list-table:: Plugins for :class:`.PermutationGate` (key = ``"permutation"``)
:header-rows: 1
* - Plugin name
- Plugin class
- Targeted connectivity
- Description
* - ``"basic"``
- :class:`~.BasicSynthesisPermutation`
- all-to-all
- optimal SWAP-count; used in ``"default"``
* - ``"acg"``
- :class:`~.ACGSynthesisPermutation`
- all-to-all
- guarantees SWAP-depth of at most `2`
* - ``"kms"``
- :class:`~.KMSSynthesisPermutation`
- linear
- many SWAP-gates, but guarantees SWAP-depth of at most `n`
* - ``"token_swapper"``
- :class:`~.TokenSwapperSynthesisPermutation`
- any
- greedily optimizes SWAP-count for arbitrary connectivity
* - ``"default"``
- :class:`~.BasicSynthesisPermutation`
- all-to-all
- best for optimizing SWAP-count
.. autosummary::
:toctree: ../stubs/
BasicSynthesisPermutation
ACGSynthesisPermutation
KMSSynthesisPermutation
TokenSwapperSynthesisPermutation
"""
from typing import Optional, Union, List, Tuple
import numpy as np
import rustworkx as rx
from qiskit.circuit.operation import Operation
from qiskit.converters import circuit_to_dag, dag_to_circuit
from qiskit.transpiler.basepasses import TransformationPass
from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.circuit import ControlFlowOp, ControlledGate, EquivalenceLibrary
from qiskit.circuit.library import LinearFunction
from qiskit.transpiler.passes.utils import control_flow
from qiskit.transpiler.target import Target
from qiskit.transpiler.coupling import CouplingMap
from qiskit.dagcircuit.dagcircuit import DAGCircuit
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.passes.routing.algorithms import ApproximateTokenSwapper
from qiskit.circuit.annotated_operation import (
AnnotatedOperation,
InverseModifier,
ControlModifier,
PowerModifier,
)
from qiskit.synthesis.clifford import (
synth_clifford_full,
synth_clifford_layers,
synth_clifford_depth_lnn,
synth_clifford_greedy,
synth_clifford_ag,
synth_clifford_bm,
)
from qiskit.synthesis.linear import (
synth_cnot_count_full_pmh,
synth_cnot_depth_line_kms,
calc_inverse_matrix,
)
from qiskit.synthesis.linear.linear_circuits_utils import transpose_cx_circ
from qiskit.synthesis.permutation import (
synth_permutation_basic,
synth_permutation_acg,
synth_permutation_depth_lnn_kms,
)
from .plugin import HighLevelSynthesisPluginManager, HighLevelSynthesisPlugin
class HLSConfig:
"""The high-level-synthesis config allows to specify a list of "methods" used by
:class:`~.HighLevelSynthesis` transformation pass to synthesize different types
of higher-level objects.
A higher-level object is an object of type :class:`~.Operation` (e.g., :class:`.Clifford` or
:class:`.LinearFunction`). Each object is referred to by its :attr:`~.Operation.name` field
(e.g., ``"clifford"`` for :class:`.Clifford` objects), and the applicable synthesis methods are
tied to this name.
In the config, each method is specified in one of several ways:
1. a tuple consisting of the name of a known synthesis plugin and a dictionary providing
additional arguments for the algorithm.
2. a tuple consisting of an instance of :class:`.HighLevelSynthesisPlugin` and additional
arguments for the algorithm.
3. a single string of a known synthesis plugin
4. a single instance of :class:`.HighLevelSynthesisPlugin`.
The following example illustrates different ways how a config file can be created::
from qiskit.transpiler.passes.synthesis.high_level_synthesis import HLSConfig
from qiskit.transpiler.passes.synthesis.high_level_synthesis import ACGSynthesisPermutation
# All the ways to specify hls_config are equivalent
hls_config = HLSConfig(permutation=[("acg", {})])
hls_config = HLSConfig(permutation=["acg"])
hls_config = HLSConfig(permutation=[(ACGSynthesisPermutation(), {})])
hls_config = HLSConfig(permutation=[ACGSynthesisPermutation()])
The names of the synthesis plugins should be declared in ``entry-points`` table for
``qiskit.synthesis`` in ``pyproject.toml``, in the form
<higher-level-object-name>.<synthesis-method-name>.
The standard higher-level-objects are recommended to have a synthesis method
called "default", which would be called automatically when synthesizing these objects,
without having to explicitly set these methods in the config.
To avoid synthesizing a given higher-level-object, one can give it an empty list of methods.
For an explicit example of using such config files, refer to the documentation for
:class:`~.HighLevelSynthesis`.
For an overview of the complete process of using high-level synthesis, see
:ref:`using-high-level-synthesis-plugins`.
"""
def __init__(self, use_default_on_unspecified=True, **kwargs):
"""Creates a high-level-synthesis config.
Args:
use_default_on_unspecified (bool): if True, every higher-level-object without an
explicitly specified list of methods will be synthesized using the "default"
algorithm if it exists.
kwargs: a dictionary mapping higher-level-objects to lists of synthesis methods.
"""
self.use_default_on_unspecified = use_default_on_unspecified
self.methods = {}
for key, value in kwargs.items():
self.set_methods(key, value)
def set_methods(self, hls_name, hls_methods):
"""Sets the list of synthesis methods for a given higher-level-object. This overwrites
the lists of methods if also set previously."""
self.methods[hls_name] = hls_methods
# ToDo: Do we have a way to specify optimization criteria (e.g., 2q gate count vs. depth)?
class HighLevelSynthesis(TransformationPass):
"""Synthesize higher-level objects and unroll custom definitions.
The input to this pass is a DAG that may contain higher-level objects,
including abstract mathematical objects (e.g., objects of type :class:`.LinearFunction`),
annotated operations (objects of type :class:`.AnnotatedOperation`), and
custom gates.
In the most common use-case when either ``basis_gates`` or ``target`` is specified,
all higher-level objects are synthesized, so the output is a :class:`.DAGCircuit`
without such objects.
More precisely, every gate in the output DAG is either directly supported by the target,
or is in ``equivalence_library``.
The abstract mathematical objects are synthesized using synthesis plugins, applying
synthesis methods specified in the high-level-synthesis config (refer to the documentation
for :class:`~.HLSConfig`).
As an example, let us assume that ``op_a`` and ``op_b`` are names of two higher-level objects,
that ``op_a``-objects have two synthesis methods ``default`` which does require any additional
parameters and ``other`` with two optional integer parameters ``option_1`` and ``option_2``,
that ``op_b``-objects have a single synthesis method ``default``, and ``qc`` is a quantum
circuit containing ``op_a`` and ``op_b`` objects. The following code snippet::
hls_config = HLSConfig(op_b=[("other", {"option_1": 7, "option_2": 4})])
pm = PassManager([HighLevelSynthesis(hls_config=hls_config)])
transpiled_qc = pm.run(qc)
shows how to run the alternative synthesis method ``other`` for ``op_b``-objects, while using the
``default`` methods for all other high-level objects, including ``op_a``-objects.
The annotated operations (consisting of a base operation and a list of inverse, control and power
modifiers) are synthesizing recursively, first synthesizing the base operation, and then applying
synthesis methods for creating inverted, controlled, or powered versions of that).
The custom gates are synthesized by recursively unrolling their definitions, until every gate
is either supported by the target or is in the equivalence library.
When neither ``basis_gates`` nor ``target`` is specified, the pass synthesizes only the top-level
abstract mathematical objects and annotated operations, without descending into the gate
``definitions``. This is consistent with the older behavior of the pass, allowing to synthesize
some higher-level objects using plugins and leaving the other gates untouched.
"""
def __init__(
self,
hls_config: Optional[HLSConfig] = None,
coupling_map: Optional[CouplingMap] = None,
target: Optional[Target] = None,
use_qubit_indices: bool = False,
equivalence_library: Optional[EquivalenceLibrary] = None,
basis_gates: Optional[List[str]] = None,
min_qubits: int = 0,
):
"""
HighLevelSynthesis initializer.
Args:
hls_config: Optional, the high-level-synthesis config that specifies synthesis methods
and parameters for various high-level-objects in the circuit. If it is not specified,
the default synthesis methods and parameters will be used.
coupling_map: Optional, directed graph represented as a coupling map.
target: Optional, the backend target to use for this pass. If it is specified,
it will be used instead of the coupling map.
use_qubit_indices: a flag indicating whether this synthesis pass is running before or after
the layout is set, that is, whether the qubit indices of higher-level-objects correspond
to qubit indices on the target backend.
equivalence_library: The equivalence library used (instructions in this library will not
be unrolled by this pass).
basis_gates: Optional, target basis names to unroll to, e.g. `['u3', 'cx']`.
Ignored if ``target`` is also specified.
min_qubits: The minimum number of qubits for operations in the input
dag to translate.
"""
super().__init__()
if hls_config is not None:
self.hls_config = hls_config
else:
# When the config file is not provided, we will use the "default" method
# to synthesize Operations (when available).
self.hls_config = HLSConfig(True)
self.hls_plugin_manager = HighLevelSynthesisPluginManager()
self._coupling_map = coupling_map
self._target = target
self._use_qubit_indices = use_qubit_indices
if target is not None:
self._coupling_map = self._target.build_coupling_map()
self._equiv_lib = equivalence_library
self._basis_gates = basis_gates
self._min_qubits = min_qubits
self._top_level_only = self._basis_gates is None and self._target is None
# include path for when target exists but target.num_qubits is None (BasicSimulator)
if not self._top_level_only and (self._target is None or self._target.num_qubits is None):
basic_insts = {"measure", "reset", "barrier", "snapshot", "delay"}
self._device_insts = basic_insts | set(self._basis_gates)
def run(self, dag: DAGCircuit) -> DAGCircuit:
"""Run the HighLevelSynthesis pass on `dag`.
Args:
dag: input dag.
Returns:
Output dag with higher-level operations synthesized.
Raises:
TranspilerError: when the transpiler is unable to synthesize the given DAG
(for instance, when the specified synthesis method is not available).
"""
# copy dag_op_nodes because we are modifying the DAG below
dag_op_nodes = dag.op_nodes()
for node in dag_op_nodes:
if isinstance(node.op, ControlFlowOp):
node.op = control_flow.map_blocks(self.run, node.op)
continue
if getattr(node.op, "_directive", False):
continue
if dag.has_calibration_for(node) or len(node.qargs) < self._min_qubits:
continue
qubits = (
[dag.find_bit(x).index for x in node.qargs] if self._use_qubit_indices else None
)
decomposition, modified = self._recursively_handle_op(node.op, qubits)
if not modified:
continue
if isinstance(decomposition, QuantumCircuit):
dag.substitute_node_with_dag(
node, circuit_to_dag(decomposition, copy_operations=False)
)
elif isinstance(decomposition, DAGCircuit):
dag.substitute_node_with_dag(node, decomposition)
elif isinstance(decomposition, Operation):
dag.substitute_node(node, decomposition)
return dag
def _recursively_handle_op(
self, op: Operation, qubits: Optional[List] = None
) -> Tuple[Union[QuantumCircuit, DAGCircuit, Operation], bool]:
"""Recursively synthesizes a single operation.
Note: the reason that this function accepts an operation and not a dag node
is that it's also used for synthesizing the base operation for an annotated
gate (i.e. no dag node is available).
There are several possible results:
- The given operation is unchanged: e.g., it is supported by the target or is
in the equivalence library
- The result is a quantum circuit: e.g., synthesizing Clifford using plugin
- The result is a DAGCircuit: e.g., when unrolling custom gates
- The result is an Operation: e.g., adding control to CXGate results in CCXGate
- The given operation could not be synthesized, raising a transpiler error
The function returns the result of the synthesis (either a quantum circuit or
an Operation), and, as an optimization, a boolean indicating whether
synthesis did anything.
The function is recursive, for example synthesizing an annotated operation
involves synthesizing its "base operation" which might also be
an annotated operation.
"""
# Try to apply plugin mechanism
decomposition = self._synthesize_op_using_plugins(op, qubits)
if decomposition is not None:
return decomposition, True
# Handle annotated operations
decomposition = self._synthesize_annotated_op(op)
if decomposition:
return decomposition, True
# Don't do anything else if processing only top-level
if self._top_level_only:
return op, False
# For non-controlled-gates, check if it's already supported by the target
# or is in equivalence library
controlled_gate_open_ctrl = isinstance(op, ControlledGate) and op._open_ctrl
if not controlled_gate_open_ctrl:
qargs = tuple(qubits) if qubits is not None else None
# include path for when target exists but target.num_qubits is None (BasicSimulator)
inst_supported = (
self._target.instruction_supported(
operation_name=op.name,
qargs=qargs,
)
if self._target is not None and self._target.num_qubits is not None
else op.name in self._device_insts
)
if inst_supported or (self._equiv_lib is not None and self._equiv_lib.has_entry(op)):
return op, False
try:
# extract definition
definition = op.definition
except TypeError as err:
raise TranspilerError(
f"HighLevelSynthesis was unable to extract definition for {op.name}: {err}"
) from err
except AttributeError:
# definition is None
definition = None
if definition is None:
raise TranspilerError(f"HighLevelSynthesis was unable to synthesize {op}.")
dag = circuit_to_dag(definition, copy_operations=False)
dag = self.run(dag)
return dag, True
def _synthesize_op_using_plugins(
self, op: Operation, qubits: List
) -> Union[QuantumCircuit, None]:
"""
Attempts to synthesize op using plugin mechanism.
Returns either the synthesized circuit or None (which occurs when no
synthesis methods are available or specified).
"""
hls_plugin_manager = self.hls_plugin_manager
if op.name in self.hls_config.methods.keys():
# the operation's name appears in the user-provided config,
# we use the list of methods provided by the user
methods = self.hls_config.methods[op.name]
elif (
self.hls_config.use_default_on_unspecified
and "default" in hls_plugin_manager.method_names(op.name)
):
# the operation's name does not appear in the user-specified config,
# we use the "default" method when instructed to do so and the "default"
# method is available
methods = ["default"]
else:
methods = []
for method in methods:
# There are two ways to specify a synthesis method. The more explicit
# way is to specify it as a tuple consisting of a synthesis algorithm and a
# list of additional arguments, e.g.,
# ("kms", {"all_mats": 1, "max_paths": 100, "orig_circuit": 0}), or
# ("pmh", {}).
# When the list of additional arguments is empty, one can also specify
# just the synthesis algorithm, e.g.,
# "pmh".
if isinstance(method, tuple):
plugin_specifier, plugin_args = method
else:
plugin_specifier = method
plugin_args = {}
# There are two ways to specify a synthesis algorithm being run,
# either by name, e.g. "kms" (which then should be specified in entry_points),
# or directly as a class inherited from HighLevelSynthesisPlugin (which then
# does not need to be specified in entry_points).
if isinstance(plugin_specifier, str):
if plugin_specifier not in hls_plugin_manager.method_names(op.name):
raise TranspilerError(
"Specified method: %s not found in available plugins for %s"
% (plugin_specifier, op.name)
)
plugin_method = hls_plugin_manager.method(op.name, plugin_specifier)
else:
plugin_method = plugin_specifier
decomposition = plugin_method.run(
op,
coupling_map=self._coupling_map,
target=self._target,
qubits=qubits,
**plugin_args,
)
# The synthesis methods that are not suited for the given higher-level-object
# will return None, in which case the next method in the list will be used.
if decomposition is not None:
return decomposition
return None
def _synthesize_annotated_op(self, op: Operation) -> Union[Operation, None]:
"""
Recursively synthesizes annotated operations.
Returns either the synthesized operation or None (which occurs when the operation
is not an annotated operation).
"""
if isinstance(op, AnnotatedOperation):
# Recursively handle the base operation
# This results in QuantumCircuit, DAGCircuit or Gate
synthesized_op, _ = self._recursively_handle_op(op.base_op, qubits=None)
if isinstance(synthesized_op, AnnotatedOperation):
raise TranspilerError(
"HighLevelSynthesis failed to synthesize the base operation of"
" an annotated operation."
)
for modifier in op.modifiers:
# If we have a DAGCircuit at this point, convert it to QuantumCircuit
if isinstance(synthesized_op, DAGCircuit):
synthesized_op = dag_to_circuit(synthesized_op, copy_operations=False)
if isinstance(modifier, InverseModifier):
# Both QuantumCircuit and Gate have inverse method
synthesized_op = synthesized_op.inverse()
elif isinstance(modifier, ControlModifier):
# Both QuantumCircuit and Gate have control method, however for circuits
# it is more efficient to avoid constructing the controlled quantum circuit.
if isinstance(synthesized_op, QuantumCircuit):
synthesized_op = synthesized_op.to_gate()
synthesized_op = synthesized_op.control(
num_ctrl_qubits=modifier.num_ctrl_qubits,
label=None,
ctrl_state=modifier.ctrl_state,
annotated=False,
)
if isinstance(synthesized_op, AnnotatedOperation):
raise TranspilerError(
"HighLevelSynthesis failed to synthesize the control modifier."
)
# Unrolling
synthesized_op, _ = self._recursively_handle_op(synthesized_op)
elif isinstance(modifier, PowerModifier):
# QuantumCircuit has power method, and Gate needs to be converted
# to a quantum circuit.
if isinstance(synthesized_op, QuantumCircuit):
qc = synthesized_op
else:
qc = QuantumCircuit(synthesized_op.num_qubits, synthesized_op.num_clbits)
qc.append(
synthesized_op,
range(synthesized_op.num_qubits),
range(synthesized_op.num_clbits),
)
qc = qc.power(modifier.power)
synthesized_op = qc.to_gate()
# Unrolling
synthesized_op, _ = self._recursively_handle_op(synthesized_op)
else:
raise TranspilerError(f"Unknown modifier {modifier}.")
return synthesized_op
return None
class DefaultSynthesisClifford(HighLevelSynthesisPlugin):
"""The default clifford synthesis plugin.
For N <= 3 qubits this is the optimal CX cost decomposition by Bravyi, Maslov.
For N > 3 qubits this is done using the general non-optimal greedy compilation
routine from reference by Bravyi, Hu, Maslov, Shaydulin.
This plugin name is :``clifford.default`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
decomposition = synth_clifford_full(high_level_object)
return decomposition
class AGSynthesisClifford(HighLevelSynthesisPlugin):
"""Clifford synthesis plugin based on the Aaronson-Gottesman method.
This plugin name is :``clifford.ag`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
decomposition = synth_clifford_ag(high_level_object)
return decomposition
class BMSynthesisClifford(HighLevelSynthesisPlugin):
"""Clifford synthesis plugin based on the Bravyi-Maslov method.
The method only works on Cliffords with at most 3 qubits, for which it
constructs the optimal CX cost decomposition.
This plugin name is :``clifford.bm`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
if high_level_object.num_qubits <= 3:
decomposition = synth_clifford_bm(high_level_object)
else:
decomposition = None
return decomposition
class GreedySynthesisClifford(HighLevelSynthesisPlugin):
"""Clifford synthesis plugin based on the greedy synthesis
Bravyi-Hu-Maslov-Shaydulin method.
This plugin name is :``clifford.greedy`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
decomposition = synth_clifford_greedy(high_level_object)
return decomposition
class LayerSynthesisClifford(HighLevelSynthesisPlugin):
"""Clifford synthesis plugin based on the Bravyi-Maslov method
to synthesize Cliffords into layers.
This plugin name is :``clifford.layers`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
decomposition = synth_clifford_layers(high_level_object)
return decomposition
class LayerLnnSynthesisClifford(HighLevelSynthesisPlugin):
"""Clifford synthesis plugin based on the Bravyi-Maslov method
to synthesize Cliffords into layers, with each layer synthesized
adhering to LNN connectivity.
This plugin name is :``clifford.lnn`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Clifford."""
decomposition = synth_clifford_depth_lnn(high_level_object)
return decomposition
class DefaultSynthesisLinearFunction(HighLevelSynthesisPlugin):
"""The default linear function synthesis plugin.
This plugin name is :``linear_function.default`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given LinearFunction."""
decomposition = synth_cnot_count_full_pmh(high_level_object.linear)
return decomposition
class KMSSynthesisLinearFunction(HighLevelSynthesisPlugin):
"""Linear function synthesis plugin based on the Kutin-Moulton-Smithline method.
This plugin name is :``linear_function.kms`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
The plugin supports the following plugin-specific options:
* use_inverted: Indicates whether to run the algorithm on the inverse matrix
and to invert the synthesized circuit.
In certain cases this provides a better decomposition than the direct approach.
* use_transposed: Indicates whether to run the algorithm on the transposed matrix
and to invert the order of CX gates in the synthesized circuit.
In certain cases this provides a better decomposition than the direct approach.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given LinearFunction."""
if not isinstance(high_level_object, LinearFunction):
raise TranspilerError(
"PMHSynthesisLinearFunction only accepts objects of type LinearFunction"
)
use_inverted = options.get("use_inverted", False)
use_transposed = options.get("use_transposed", False)
mat = high_level_object.linear.astype(int)
if use_transposed:
mat = np.transpose(mat)
if use_inverted:
mat = calc_inverse_matrix(mat)
decomposition = synth_cnot_depth_line_kms(mat)
if use_transposed:
decomposition = transpose_cx_circ(decomposition)
if use_inverted:
decomposition = decomposition.inverse()
return decomposition
class PMHSynthesisLinearFunction(HighLevelSynthesisPlugin):
"""Linear function synthesis plugin based on the Patel-Markov-Hayes method.
This plugin name is :``linear_function.pmh`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
The plugin supports the following plugin-specific options:
* section size: The size of each section used in the Patel–Markov–Hayes algorithm [1].
* use_inverted: Indicates whether to run the algorithm on the inverse matrix
and to invert the synthesized circuit.
In certain cases this provides a better decomposition than the direct approach.
* use_transposed: Indicates whether to run the algorithm on the transposed matrix
and to invert the order of CX gates in the synthesized circuit.
In certain cases this provides a better decomposition than the direct approach.
References:
1. Patel, Ketan N., Igor L. Markov, and John P. Hayes,
*Optimal synthesis of linear reversible circuits*,
Quantum Information & Computation 8.3 (2008): 282-294.
`arXiv:quant-ph/0302002 [quant-ph] <https://arxiv.org/abs/quant-ph/0302002>`_
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given LinearFunction."""
if not isinstance(high_level_object, LinearFunction):
raise TranspilerError(
"PMHSynthesisLinearFunction only accepts objects of type LinearFunction"
)
section_size = options.get("section_size", 2)
use_inverted = options.get("use_inverted", False)
use_transposed = options.get("use_transposed", False)
mat = high_level_object.linear.astype(int)
if use_transposed:
mat = np.transpose(mat)
if use_inverted:
mat = calc_inverse_matrix(mat)
decomposition = synth_cnot_count_full_pmh(mat, section_size=section_size)
if use_transposed:
decomposition = transpose_cx_circ(decomposition)
if use_inverted:
decomposition = decomposition.inverse()
return decomposition
class KMSSynthesisPermutation(HighLevelSynthesisPlugin):
"""The permutation synthesis plugin based on the Kutin, Moulton, Smithline method.
This plugin name is :``permutation.kms`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Permutation."""
decomposition = synth_permutation_depth_lnn_kms(high_level_object.pattern)
return decomposition
class BasicSynthesisPermutation(HighLevelSynthesisPlugin):
"""The permutation synthesis plugin based on sorting.
This plugin name is :``permutation.basic`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Permutation."""
decomposition = synth_permutation_basic(high_level_object.pattern)
return decomposition
class ACGSynthesisPermutation(HighLevelSynthesisPlugin):
"""The permutation synthesis plugin based on the Alon, Chung, Graham method.
This plugin name is :``permutation.acg`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Permutation."""
decomposition = synth_permutation_acg(high_level_object.pattern)
return decomposition
class TokenSwapperSynthesisPermutation(HighLevelSynthesisPlugin):
"""The permutation synthesis plugin based on the token swapper algorithm.
This plugin name is :``permutation.token_swapper`` which can be used as the key on
an :class:`~.HLSConfig` object to use this method with :class:`~.HighLevelSynthesis`.
In more detail, this plugin is used to synthesize objects of type `PermutationGate`.
When synthesis succeeds, the plugin outputs a quantum circuit consisting only of swap
gates. When synthesis does not succeed, the plugin outputs `None`.
If either `coupling_map` or `qubits` is None, then the synthesized circuit
is not required to adhere to connectivity constraints, as is the case
when the synthesis is done before layout/routing.
On the other hand, if both `coupling_map` and `qubits` are specified, the synthesized
circuit is supposed to adhere to connectivity constraints. At the moment, the
plugin only creates swap gates between qubits in `qubits`, i.e. it does not use
any other qubits in the coupling map (if such synthesis is not possible, the
plugin outputs `None`).
The plugin supports the following plugin-specific options:
* trials: The number of trials for the token swapper to perform the mapping. The
circuit with the smallest number of SWAPs is returned.
* seed: The argument to the token swapper specifying the seed for random trials.
* parallel_threshold: The argument to the token swapper specifying the number of nodes
in the graph beyond which the algorithm will use parallel processing.
For more details on the token swapper algorithm, see to the paper:
`arXiv:1902.09102 <https://arxiv.org/abs/1902.09102>`__.
"""
def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **options):
"""Run synthesis for the given Permutation."""
trials = options.get("trials", 5)
seed = options.get("seed", 0)
parallel_threshold = options.get("parallel_threshold", 50)
pattern = high_level_object.pattern
pattern_as_dict = {j: i for i, j in enumerate(pattern)}
# When the plugin is called from the HighLevelSynthesis transpiler pass,
# the coupling map already takes target into account.
if coupling_map is None or qubits is None:
# The abstract synthesis uses a fully connected coupling map, allowing
# arbitrary connections between qubits.
used_coupling_map = CouplingMap.from_full(len(pattern))
else:
# The concrete synthesis uses the coupling map restricted to the set of
# qubits over which the permutation gate is defined. If we allow using other
# qubits in the coupling map, replacing the node in the DAGCircuit that
# defines this PermutationGate by the DAG corresponding to the constructed
# decomposition becomes problematic. Note that we allow the reduced
# coupling map to be disconnected.
used_coupling_map = coupling_map.reduce(qubits, check_if_connected=False)
graph = used_coupling_map.graph.to_undirected()
swapper = ApproximateTokenSwapper(graph, seed=seed)
try:
swapper_result = swapper.map(
pattern_as_dict, trials, parallel_threshold=parallel_threshold
)
except rx.InvalidMapping:
swapper_result = None
if swapper_result is not None:
decomposition = QuantumCircuit(len(graph.node_indices()))
for swap in swapper_result:
decomposition.swap(*swap)
return decomposition
return None