Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
partition.py
Go to the documentation of this file.
1 from typing import Literal
2 import functools
3 import numpy as np
4 
5 from squander.gates.qgd_Circuit import qgd_Circuit as Circuit
6 from squander import utils
7 
8 from squander.partitioning.kahn import kahn_partition
9 from squander.partitioning.ilp import max_partitions
10 from squander.partitioning.tdag import tdag_max_partitions
11 from squander.partitioning.tools import (
12  translate_param_order,
13  get_qiskit_partitions,
14  get_qiskit_fusion_partitions,
15  get_bqskit_partitions,
16 )
17 
18 PartitionStrategy = Literal[
19  "kahn",
20  "ilp",
21  "ilp-fusion",
22  "ilp-fusion-ca",
23  "tdag",
24  "qiskit",
25  "qiskit-fusion",
26  "bqskit-Quick",
27  "bqskit-Scan",
28  "bqskit-Greedy",
29  "bqskit-Cluster",
30 ]
31 
32 PARTITION_FUNCTIONS = {
33  "kahn": kahn_partition,
34  "ilp": max_partitions,
35  "ilp-fusion": functools.partial(max_partitions, fusion_cost=True),
36  "ilp-fusion-ca": functools.partial(
37  max_partitions, fusion_cost=True, control_aware=True
38  ),
39  "tdag": tdag_max_partitions,
40  "gtqcp": functools.partial(tdag_max_partitions, use_gtqcp=True),
41  "qiskit": get_qiskit_partitions,
42  "qiskit-fusion": get_qiskit_fusion_partitions,
43  "bqskit-Quick": functools.partial(get_bqskit_partitions, partitioner="Quick"),
44  "bqskit-Scan": functools.partial(get_bqskit_partitions, partitioner="Scan"),
45  "bqskit-Greedy": functools.partial(get_bqskit_partitions, partitioner="Greedy"),
46  "bqskit-Cluster": functools.partial(get_bqskit_partitions, partitioner="Cluster"),
47 }
48 
49 
51  circ: Circuit,
52  parameters: np.ndarray,
53  max_partition_size: int,
54  strategy: PartitionStrategy = "kahn",
55  filename=None,
56 ) -> tuple[Circuit, np.ndarray, list[int]]:
57  """
58  Call to partition a circuit
59 
60  Args:
61 
62  circ ( Circuit ) A circuit to be partitioned
63 
64  parameters ( np.ndarray ) A parameter array associated with the input circuit
65 
66  max_partition_size (int) : The maximal number of qubits in the partitions
67 
68  strategy (PartitionStrategy, optional) Set to ILP (slow, but giving optimal result), TDAG, or KAHN (default)
69 
70  Return:
71 
72  Returns with the paritioned circuit and the associated parameter array. Partitions are organized into subcircuits of the resulting circuit
73  """
74 
75  func = PARTITION_FUNCTIONS.get(strategy, kahn_partition)
76  if strategy in [
77  "qiskit",
78  "qiskit-fusion",
79  "bqskit-Quick",
80  "bqskit-Scan",
81  "bqskit-Greedy",
82  "bqskit-Cluster",
83  ]:
84  parameters, partitioned_circ, param_order, L = func(
85  filename, max_partition_size
86  )
87  else:
88  partitioned_circ, param_order, L = func(circ, max_partition_size)
89 
90  param_reordered = translate_param_order(parameters, param_order)
91  return partitioned_circ, param_reordered, L
92 
93 
95  filename: str, max_partition_size: int, strategy: PartitionStrategy = "kahn"
96 ) -> tuple[Circuit, np.ndarray, list[int]]:
97  """
98  Call to partition a circuit loaded from a qasm file
99 
100  Args:
101 
102  filename ( str ) A path to a qasm file to be imported
103 
104  max_partition_size (int) : The maximal number of qubits in the partitions
105 
106  strategy (PartitionStrategy, optional) Set to ilp (slow, but giving optimal result), tdag, or kahn (default)
107 
108  Return:
109 
110  Returns with the paritioned circuit and the associated parameter array. Partitions are organized into subcircuits of the resulting circuit
111  """
112 
113  circ, parameters, _ = utils.qasm_to_squander_circuit(filename)
114  return PartitionCircuit(circ, parameters, max_partition_size, strategy, filename)
115 
116 
117 if __name__ == "__main__":
118  PartitionCircuitQasm("examples/partitioning/qasm_samples/heisenberg-16-20.qasm", 4)
def PartitionCircuitQasm
Definition: partition.py:95
def PartitionCircuit
Definition: partition.py:51
def translate_param_order
Definition: tools.py:112