QNLP  v1.0
PyQNLPSimulator.UnitaryFinder Class Reference
Collaboration diagram for PyQNLPSimulator.UnitaryFinder:
Collaboration graph

Public Member Functions

def __init__ (self, simulator, unitary)
 
def findOps (self, gate_order, cost_tol=1e-3)
 
def genOps (self, list gates, depth=5)
 

Data Fields

 ls
 
 minFunc
 
 unitary
 
 unitarynp
 
 gates
 
 gate_ops
 

Detailed Description

Definition at line 74 of file __init__.py.

Constructor & Destructor Documentation

◆ __init__()

def PyQNLPSimulator.UnitaryFinder.__init__ (   self,
  simulator,
  unitary 
)

Definition at line 75 of file __init__.py.

75  def __init__(self, simulator, unitary):
76  from scipy.optimize import least_squares as lsq
77  self.ls = lsq
78  self.minFunc = None
79  self.unitary = unitary
80  self.unitarynp = None
81  self.gates = ["X", "Y", "Z", "H", "RX", "RY", "RZ", "RZPh"]
82  self.gate_ops = GateOps(simulator)
83 

Member Function Documentation

◆ findOps()

def PyQNLPSimulator.UnitaryFinder.findOps (   self,
  gate_order,
  cost_tol = 1e-3 
)

Definition at line 84 of file __init__.py.

84  def findOps(self, gate_order, cost_tol = 1e-3):
85  lambda x: np.array([2*x[0] + x[1] - 1])
86  num_params = 0
87  for i in gate_order:
88  if "R" in i[0]:
89  num_params += 1
90 
91  def setMinFunc(x, gates):
92  mm = self.gate_ops.createU(self.unitary).as_numpy()
93 
94  nn = 1
95  param_val = 0
96  for g in reversed(gates):
97  if "R" in g[0]:
98  nn *= getattr(self.gate_ops , g)(x[param_val])
99  param_val += 1
100  else:
101  nn *= getattr(self.gate_ops, "".join((g,"np")))
102  nn = nn.tolist()
103 
104  ml = [i for sublist in mm for i in sublist]
105  mr = [np.real(i) for i in ml]
106  mi = [np.imag(i) for i in ml]
107  m = [] + mr + mi
108  nl = [i for sublist in nn for i in sublist]
109  nr = [np.real(i) for i in nl]
110  ni = [np.imag(i) for i in nl]
111  n = [] + nr + ni
112 
113  ab = np.sum([i**2 for i in np.subtract(m,n)])
114 
115  return ab
116 
117  if num_params > 0:
118  res = least_squares(setMinFunc, [0]*num_params, args=(gate_order,), bounds=(-np.pi, np.pi))
119  if res.cost <= cost_tol:
120  return res.success, res.x
121  else:
122  return False, []
123 
124  else:
125  rr = setMinFunc([], gate_order)
126  return np.isclose(rr, 0, rtol=cost_tol), []
127 

References PyQNLPSimulator.UnitaryFinder.gate_ops, and PyQNLPSimulator.UnitaryFinder.unitary.

◆ genOps()

def PyQNLPSimulator.UnitaryFinder.genOps (   self,
list  gates,
  depth = 5 
)
Generate the combinations of operations.
Same gate is never called twice in a row.

Definition at line 128 of file __init__.py.

128  def genOps(self, gates : list, depth=5):
129  """Generate the combinations of operations.
130  Same gate is never called twice in a row."""
131 
132  ops_list0 = list(product(gates, repeat=depth))
133  ops_list = list(product(gates, repeat=depth))
134  del_idx = set()
135  for idx_ops_list, ops in enumerate(ops_list):
136  for idx_ops in range(1,len(ops)):
137  if ops[idx_ops] == ops[idx_ops-1]:
138  #del ops_list[idx_ops_list]
139  del_idx.add(idx_ops_list)
140  ops_list = [val for idx,val in enumerate(ops_list) if idx not in del_idx]
141  return ops_list

Field Documentation

◆ gate_ops

PyQNLPSimulator.UnitaryFinder.gate_ops

Definition at line 82 of file __init__.py.

Referenced by PyQNLPSimulator.UnitaryFinder.findOps().

◆ gates

PyQNLPSimulator.UnitaryFinder.gates

Definition at line 81 of file __init__.py.

◆ ls

PyQNLPSimulator.UnitaryFinder.ls

Definition at line 77 of file __init__.py.

◆ minFunc

PyQNLPSimulator.UnitaryFinder.minFunc

Definition at line 78 of file __init__.py.

◆ unitary

PyQNLPSimulator.UnitaryFinder.unitary

Definition at line 79 of file __init__.py.

Referenced by PyQNLPSimulator.UnitaryFinder.findOps().

◆ unitarynp

PyQNLPSimulator.UnitaryFinder.unitarynp

Definition at line 80 of file __init__.py.


The documentation for this class was generated from the following file: