Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
example_get_circuit_unitary.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 """
3 Created on Fri Jun 26 14:42:56 2020
4 Copyright 2020 Peter Rakyta, Ph.D.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 @author: Peter Rakyta, Ph.D.
19 """
20 
22 
23 
24 from squander import N_Qubit_Decomposition_adaptive
25 
26 
27 import numpy as np
28 import random
29 
30 
31 # The gate stucture created by the adaptive decomposition class reads as:
32 # [ U3, U3, CRY || U3, U3, CRY || U3, U3, CRY || U3, U3, CRY || .... || U3, U3, U3, U3, ... ]
33 # The individual blocks are separated by ||. Each U3 gate has 3 free parameters, the CRY gates have 1 free parameter
34 # The first qbit_num gates are U3 transformations on each qubit.
35 # In the quantum circuit the operations on the unitary Umtx are performed in the following order:
36 # U3*U3*...*U3 * CRY*U3*U3 * CRY*U3*U3 * CRY*U3*U3 * ... * CRY*U3*U3 * Umtx
37 
38 
39 # the ratio of nontrivial 2-qubit building blocks
40 nontrivial_ratio = 0.5
41 
42 # number of qubits
43 qbit_num = 3
44 
45 # matrix size of the unitary
46 matrix_size = pow(2, qbit_num )
47 
48 # number of adaptive levels
49 level = 2
50 
51 
52 
55 def create_randomized_parameters( num_of_parameters ):
56 
57 
58  parameters = np.zeros(num_of_parameters)
59 
60  # the number of adaptive layers in one level
61  num_of_adaptive_layers = qbit_num*(qbit_num-1)
62  parameters[0:qbit_num*3] = (2*np.random.rand(qbit_num*3)-1)*2*np.pi
63 
64  # the number of nontrivial adaptive layers
65  num_nontrivial = int(nontrivial_ratio*num_of_adaptive_layers)
66 
67 
68  layer_indices = list(range(num_of_adaptive_layers))
69 
70  for idx in range(num_nontrivial):
71 
72  # randomly choose an adaptive layer to be nontrivial
73  chosen_layer = random.randint(1, len(layer_indices)-1)
74  adaptive_layer_idx = layer_indices[chosen_layer]
75  layer_indices.pop( chosen_layer )
76 
77 
78  # set the radom parameters of the chosen adaptive layer
79  start_idx = qbit_num*3 + (adaptive_layer_idx-1)*7
80  end_idx = start_idx + 7
81  parameters[start_idx:end_idx] = (2*np.random.rand(7)-1)*2*np.pi
82 
83 
84  return parameters
85 
86 
87 
88 # creating a class to decompose the unitary
89 cDecompose = N_Qubit_Decomposition_adaptive( np.eye(matrix_size), level_limit_max=5, level_limit_min=0 )
90 
91 # adding decomposing layers to the gat structure
92 for idx in range(level):
93  cDecompose.add_Adaptive_Layers()
94 
95 cDecompose.add_Finalyzing_Layer_To_Gate_Structure()
96 
97 
98 # get the number of free parameters
99 num_of_parameters = cDecompose.get_Parameter_Num()
100 
101 # create randomized parameters having number of nontrivial adaptive blocks determined by the parameter nontrivial_ratio
102 parameters = create_randomized_parameters( num_of_parameters )
103 
104 # getting the unitary corresponding to quantum circuit
105 unitary = cDecompose.get_Matrix( parameters )
106 
107 print( parameters )
108 print( unitary )
109 
110 
111 
112 
def create_randomized_parameters(num_of_parameters)
Call to construct random parameter, with limited number of non-trivial adaptive layers.