Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
GQML_test.py
Go to the documentation of this file.
1 from squander import Generative_Quantum_Machine_Learning
2 import numpy as np
3 import scipy.linalg
4 import matplotlib.pyplot as plt
5 from scipy.optimize import curve_fit
6 import matplotlib
7 import dataset_generator
8 import networkx as nx
9 
10 
11 def generate_MRF_dataset(n_nodes, graph_type, dataset_size, path = None, G=None):
12  if graph_type != "custom":
13  mrf = dataset_generator.GeneralBinaryMRF(graph_type, n_nodes)
14  else:
15  mrf = dataset_generator.GeneralBinaryMRF(graph_type, n_nodes, G=G)
16  mrf_samples = np.random.choice(
17  range(2**mrf.n_vertices), size=dataset_size, p=mrf.distribution
18  )
19  training_set = np.array(
20  [
21  np.array(list(format(i, "b").zfill(mrf.n_vertices))).astype(int)
22  for i in mrf_samples
23  ]
24  )
25 
26  if path is not None:
27  mrf.save(path)
28 
29  return training_set, mrf.distribution, list(nx.find_cliques(mrf.graph))
30 
31 n_nodes = 5
32 graph_type = "custom"
33 dataset_size = 1000
34 
35 G = nx.Graph()
36 G.add_nodes_from(range(n_nodes))
37 edges = [(x, x+1) for x in range(n_nodes-1)]
38 edges.append((n_nodes-1, 0))
39 # edges = [[0, 1], [1, 2], [2, 3], [3, 4], [3, 5], [4, 5], [5, 6], [6, 7], [6, 8], [6, 9], [7, 8], [7, 9], [8, 9], [9, 1]]
40 G.add_edges_from(edges)
41 training_set, target_distribution, cliques = generate_MRF_dataset(n_nodes, graph_type, dataset_size, G=G)
42 
43 
44 # generate configuration dictionary for the solver
45 config = {"max_inner_iterations":8000,
46  "batch_size": 3,
47  "check_for_convergence": True,
48  "convergence_length": 20,
49  "output_periodicity": 500}
50 qbit_num = n_nodes
51 sigma = [0.25, 10, 1000]
52 x = training_set.astype(np.int32)
53 P_star = target_distribution
54 use_lookup_table = True
55 use_exact = True
56 print(cliques)
57 
58 GQML = Generative_Quantum_Machine_Learning(x, P_star, sigma, qbit_num, use_lookup_table, cliques, use_exact, config)
59 
60 
61 # set the optimization engine to agents
62 GQML.set_Optimizer("COSINE")
63 
64 # set the ansatz variant (U3 rotations and CNOT gates)
65 GQML.set_Ansatz("QCMRF")
66 
67 GQML.Generate_Circuit(10, 1)
68 param_num = GQML.get_Parameter_Num()
69 print(param_num)
70 
71 
72 parameters = np.zeros(param_num)
73 GQML.set_Optimized_Parameters(parameters)
74 print(GQML.get_Qiskit_Circuit())
75 
76 
77 initial_state = np.zeros( (1 << qbit_num), dtype=np.complex128 )
78 initial_state[0] = 1.0 + 0j
79 state_to_transform = initial_state.copy()
80 GQML.apply_to( parameters, state_to_transform );
81 P_theta = np.abs(state_to_transform)**2
82 print("TV",np.sum(np.abs(P_theta - P_star))/2)
83 
84 
85 tvs_qcmrf = []
86 for i in range(1):
87  parameters = np.zeros(param_num)
88  print("MMD", GQML.Optimization_Problem(parameters))
89  GQML.set_Optimized_Parameters(parameters)
90  GQML.Start_Optimization()
91  parameters = GQML.get_Optimized_Parameters()
92  initial_state = np.zeros( (1 << qbit_num), dtype=np.complex128 )
93  initial_state[0] = 1.0 + 0j
94  state_to_transform = initial_state.copy()
95  GQML.apply_to( parameters, state_to_transform );
96  P_theta = np.abs(state_to_transform)**2
97  print("TV", np.sum(np.abs(P_theta - P_star))/2)
98  tvs_qcmrf.append(np.sum(np.abs(P_theta - P_star))/2)
99 
100  plt.plot(P_star)
101  plt.plot(P_theta)
102  plt.show()
103 
104 tvs_hea = []
105 for i in range(0):
106  GQML.set_Ansatz("HEA")
107 
108  GQML.Generate_Circuit(1, 1)
109  param_num = GQML.get_Parameter_Num()
110  parameters = np.zeros(param_num)
111  print(param_num)
112  print(GQML.get_Qiskit_Circuit())
113  print("MMD", GQML.Optimization_Problem(parameters))
114  GQML.set_Optimized_Parameters(parameters)
115  GQML.Start_Optimization()
116  parameters = GQML.get_Optimized_Parameters()
117  initial_state = np.zeros( (1 << qbit_num), dtype=np.complex128 )
118  initial_state[0] = 1.0 + 0j
119  state_to_transform = initial_state.copy()
120  GQML.apply_to( parameters, state_to_transform );
121  P_theta = np.abs(state_to_transform)**2
122  print("TV", np.sum(np.abs(P_theta - P_star))/2)
123  tvs_hea.append(np.sum(np.abs(P_theta - P_star))/2)
124 
125  plt.plot(P_star)
126  plt.plot(P_theta)
127  plt.show()
def generate_MRF_dataset(n_nodes, graph_type, dataset_size, path=None, G=None)
Definition: GQML_test.py:11