Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
qgd_N_Qubit_Decomposition.py
Go to the documentation of this file.
1 
3 """
4 Created on Tue Jun 30 15:44:26 2020
5 Copyright 2020 Peter Rakyta, Ph.D.
6 
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see http://www.gnu.org/licenses/.
21 
22 @author: Peter Rakyta, Ph.D.
23 """
24 
25 
27 
28 
29 import numpy as np
30 from os import path
31 from squander.decomposition.qgd_N_Qubit_Decomposition_Wrapper import qgd_N_Qubit_Decomposition_Wrapper
32 from squander.gates.qgd_Circuit import qgd_Circuit as qgd_Circuit
33 
34 
35 
36 
38 class qgd_N_Qubit_Decomposition(qgd_N_Qubit_Decomposition_Wrapper):
39 
40 
41 
47  def __init__( self, Umtx, optimize_layer_num=False, initial_guess="RANDOM" ):
48 
49 
50  self.qbit_num = int(round( np.log2( len(Umtx) ) ))
51 
52  # call the constructor of the wrapper class
53  super(qgd_N_Qubit_Decomposition, self).__init__(Umtx, self.qbit_num, optimize_layer_num, initial_guess)
54 
55 
56 
58  def Start_Decomposition(self,):
59 
60  # call the C wrapper function
61  super(qgd_N_Qubit_Decomposition, self).Start_Decomposition()
62 
63 
64 
67  def Reorder_Qubits( self, qbit_list ):
68 
69  # call the C wrapper function
70  super(qgd_N_Qubit_Decomposition, self).Reorder_Qubits(qbit_list)
71 
72 
73 
75  def List_Gates(self):
76 
77  # call the C wrapper function
78  super(qgd_N_Qubit_Decomposition, self).List_Gates()
79 
80 
81 
83  def set_Gate_Structure( self, gate_structure_dict ):
84 
85  if isinstance(gate_structure_dict, dict) :
86 
87  for key, item in gate_structure_dict.items():
88 
89  if not isinstance(item, qgd_Circuit) :
90  raise Exception("Input parameter gate_structure_dict should be a dictionary of (int, qgd_Circuit) describing the gate structure unit cells at individual qubits")
91  return
92  else:
93  raise Exception("Input parameter gate_structure_dict should be a dictionary of (int, qgd_Circuit) describing the gate structure unit cells at individual qubits")
94  return
95 
96  super(qgd_N_Qubit_Decomposition, self).set_Gate_Structure( gate_structure_dict )
97 
98 
99 
102  def get_Circuit( self ):
103 
104  # call the C wrapper function
105  return super().get_Circuit()
106 
107 
110  def get_Qiskit_Circuit( self ):
111 
112  from squander import Qiskit_IO
113 
114  squander_circuit = self.get_Circuit()
115  parameters = self.get_Optimized_Parameters()
116 
117  return Qiskit_IO.get_Qiskit_Circuit( squander_circuit, parameters )
118 
119 
120 
121 
124  def get_Cirq_Circuit( self ):
125 
126  import cirq
127 
128 
129  # creating Cirq quantum circuit
130  circuit = cirq.Circuit()
131 
132  # creating qubit register
133  q = cirq.LineQubit.range(self.qbit_num)
134 
135  # retrive the list of decomposing gate structure
136  gates = self.get_Gates()
137 
138  # constructing quantum circuit
139  for idx in range(len(gates)-1, -1, -1):
140 
141  gate = gates[idx]
142 
143  if gate.get("type") == "CNOT":
144  # adding CNOT gate to the quantum circuit
145  circuit.append(cirq.CNOT(q[self.qbit_num-1-gate.get("control_qbit")], q[self.qbit_num-1-gate.get("target_qbit")]))
146 
147  elif gate.get("type") == "CZ":
148  # adding CZ gate to the quantum circuit
149  circuit.append(cirq.CZ(q[self.qbit_num-1-gate.get("control_qbit")], q[self.qbit_num-1-gate.get("target_qbit")]))
150 
151  elif gate.get("type") == "CH":
152  # adding CZ gate to the quantum circuit
153  circuit.append(cirq.CH(q[self.qbit_num-1-gate.get("control_qbit")], q[self.qbit_num-1-gate.get("target_qbit")]))
154 
155  elif gate.get("type") == "SYC":
156  # Sycamore gate
157  circuit.append(cirq.google.SYC(q[self.qbit_num-1-gate.get("control_qbit")], q[self.qbit_num-1-gate.get("target_qbit")]))
158 
159  elif gate.get("type") == "U3":
160  print("Unsupported gate in the Cirq export: U3 gate")
161  return None;
162 
163  elif gate.get("type") == "RX":
164  # RX gate
165  circuit.append(cirq.rx(gate.get("Theta")).on(q[self.qbit_num-1-gate.get("target_qbit")]))
166 
167  elif gate.get("type") == "RY":
168  # RY gate
169  circuit.append(cirq.ry(gate.get("Theta")).on(q[self.qbit_num-1-gate.get("target_qbit")]))
170 
171  elif gate.get("type") == "RZ":
172  # RZ gate
173  circuit.append(cirq.rz(gate.get("Phi")).on(q[self.qbit_num-1-gate.get("target_qbit")]))
174 
175  elif gate.get("type") == "X":
176  # RZ gate
177  circuit.append(cirq.x(q[self.qbit_num-1-gate.get("target_qbit")]))
178 
179  elif gate.get("type") == "SX":
180  # RZ gate
181  circuit.append(cirq.sx(q[self.qbit_num-1-gate.get("target_qbit")]))
182 
183 
184  return circuit
185 
186 
187 
188 
189 
190 
191 
192 
def get_Qiskit_Circuit(self)
Export the unitary decomposition into Qiskit format.
def Reorder_Qubits(self, qbit_list)
Call to reorder the qubits in the matrix of the gate.
def get_Cirq_Circuit(self)
Export the unitary decomposition into Qiskit format.
A QGD Python interface class for the decomposition of N-qubit unitaries into U3 and CNOT gates...
def set_Gate_Structure(self, gate_structure_dict)
Call to set the gate structure to be used in the decomposition.
def List_Gates(self)
Call to print the gates decomposing the initial unitary.
def get_Circuit(self)
Call to retrieve the incorporated quantum circuit (Squander format)
def Start_Decomposition(self)
Wrapper function to call the start_decomposition method of C++ class N_Qubit_Decomposition.
def __init__(self, Umtx, optimize_layer_num=False, initial_guess="RANDOM")
Constructor of the class.