Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_depemdency_graph.py
Go to the documentation of this file.
1 '''
2 Copyright 2020 Peter Rakyta, Ph.D.
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/.
18 
19 '''
20 
21 import numpy as np
22 
23 from qiskit import QuantumCircuit
24 from squander import utils
25 from squander import Qiskit_IO
26 
27 try:
28  from mpi4py import MPI
29  MPI_imported = True
30 except ModuleNotFoundError:
31  MPI_imported = False
32 
33 
35  """This is a test class of the python iterface to the decompsition classes of the QGD package"""
36 
37 
38  def create_flat_circuit(self, qbit_num, layer_num=2):
39  """
40  Add layers to disentangle the 3rd qubit from the others
41  linear chain with IBM native operations
42 
43  """
44 
45  from squander import Circuit
46 
47 
48  # creating an instance of the wrapper class Circuit
49  Circuit_ret = Circuit( qbit_num )
50 
51 
52  for idx in range(0,layer_num,2):
53 
54 
55  for qbit_idx in range(qbit_num-1):
56 #Rz Ry Rz = Rz Sx Rz Sx^+ Rz
57  #Layer.add_U3(2, True, True, True)
58  Circuit_ret.add_RZ( qbit_idx+1 )
59  Circuit_ret.add_SX( qbit_idx+1 )
60  Circuit_ret.add_RZ( qbit_idx+1 )
61  Circuit_ret.add_X( qbit_idx+1 )
62  Circuit_ret.add_SX( qbit_idx+1 )
63  Circuit_ret.add_RZ( qbit_idx+1 )
64 
65  #Layer.add_U3(1, True, True, True)
66  Circuit_ret.add_RZ( qbit_idx )
67  #Layer.add_RY( 1 )
68  Circuit_ret.add_SX( qbit_idx )
69  Circuit_ret.add_RZ( qbit_idx )
70  Circuit_ret.add_X( qbit_idx )
71  Circuit_ret.add_SX( qbit_idx )
72  Circuit_ret.add_RZ( qbit_idx )
73 
74  # add CNOT gate to the block
75  Circuit_ret.add_CNOT( qbit_idx, qbit_idx+1)
76 
77 
78 
79 
80  return Circuit_ret
81 
82 
83 
84  def create_circuit(self, qbit_num, layer_num=2):
85  """
86  Add layers to disentangle the 3rd qubit from the others
87  linear chain with IBM native operations
88 
89  """
90 
91  from squander import Circuit
92 
93 
94  # creating an instance of the wrapper class Circuit
95  Circuit_ret = Circuit( qbit_num )
96 
97 
98  for idx in range(0,layer_num,2):
99 
100 
101  for qbit_idx in range(qbit_num-1):
102 #Rz Ry Rz = Rz Sx Rz Sx^+ Rz
103  Layer = Circuit( qbit_num )
104 
105  Layer.add_RZ( qbit_idx+1 )
106  Layer.add_SX( qbit_idx+1 )
107  Layer.add_RZ( qbit_idx+1 )
108  Layer.add_X( qbit_idx+1 )
109  Layer.add_SX( qbit_idx+1 )
110  Layer.add_RZ( qbit_idx+1 )
111 
112 
113  Layer.add_RZ( qbit_idx )
114  #Layer.add_RY( 1 )
115  Layer.add_SX( qbit_idx )
116  Layer.add_RZ( qbit_idx )
117  Layer.add_X( qbit_idx )
118  Layer.add_SX( qbit_idx )
119  Layer.add_RZ( qbit_idx )
120 
121  # add CNOT gate to the block
122  Layer.add_CNOT( qbit_idx, qbit_idx+1)
123 
124  Circuit_ret.add_Circuit( Layer )
125 
126 
127 
128 
129 
130 
131  return Circuit_ret
132 
133 
134 
135 
136 
137 
139 
140  # the number of qubits spanning the unitary
141  qbit_num = 4
142 
143 
144  # create custom gate structure for the decomposition
145  squander_circuit = self.create_flat_circuit( qbit_num, 2 )
146 
147 
148  # cerate random parameter array for the circuit
149  parameter_num = squander_circuit.get_Parameter_Num()
150  parameters = np.random.randn( parameter_num ) *2*np.pi
151 
152  Qiskit_circuit = Qiskit_IO.get_Qiskit_Circuit( squander_circuit, parameters )
153 
154  print( ' ' )
155  print( Qiskit_circuit )
156 
157  flat_circuit = squander_circuit.get_Flat_Circuit()
158 
159  #assert( decomposition_error < 1e-3 )
160 
161 
162 
163 
164 
166 
167  # the number of qubits spanning the unitary
168  qbit_num = 4
169 
170 
171  # create custom gate structure for the decomposition
172  squander_circuit = self.create_circuit( qbit_num, 2 )
173 
174  flat_circuit = squander_circuit.get_Flat_Circuit()
175 
176 
177  # cerate random parameter array for the circuit
178  parameter_num = flat_circuit.get_Parameter_Num()
179  parameters = np.random.randn( parameter_num ) *2*np.pi
180 
181 
182  Qiskit_circuit = Qiskit_IO.get_Qiskit_Circuit( flat_circuit, parameters )
183 
184  print( ' ' )
185  print( Qiskit_circuit )
186 
187 
188  #assert( decomposition_error < 1e-3 )
189 
190 
191 
192 
193  def test_parents(self):
194 
195  # the number of qubits spanning the unitary
196  qbit_num = 4
197 
198 
199  # create custom gate structure for the decomposition
200  squander_circuit = self.create_flat_circuit( qbit_num, 2 )
201 
202 
203  gates = squander_circuit.get_Gates()
204 
205 
206  chosen_gate_idx = 18
207  chosen_gate = gates[ chosen_gate_idx ]
208 
209 
210  print( "The chosen gate is: " + str(chosen_gate) )
211 
212  parents_indices = squander_circuit.get_Parents( chosen_gate )
213  parent_gate = gates[ parents_indices[0] ]
214 
215 
216  print("The parents gate is: " + str( parent_gate ) )
217 
218 
219 
220  def test_children(self):
221 
222  # the number of qubits spanning the unitary
223  qbit_num = 4
224 
225 
226  # create custom gate structure for the decomposition
227  squander_circuit = self.create_flat_circuit( qbit_num, 2 )
228 
229 
230  gates = squander_circuit.get_Gates()
231 
232 
233  chosen_gate_idx = 18
234  chosen_gate = gates[ chosen_gate_idx ]
235 
236 
237  print( "The chosen gate is: " + str(chosen_gate) )
238 
239  children_indices = squander_circuit.get_Children( chosen_gate )
240  child_gate = gates[ children_indices[0] ]
241 
242 
243  print("The child gate is: " + str( child_gate ) )
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262