Skip to content

Commit

Permalink
Merge pull request #9 from QuantumApplicationLab/issue5_htree
Browse files Browse the repository at this point in the history
Issue5 htree
  • Loading branch information
NicoRenaud authored Nov 21, 2023
2 parents 914ceb2 + cb921f0 commit 42c5558
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
3 changes: 3 additions & 0 deletions vqls_prototype/solver/vqls.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def construct_circuit( # pylint: disable=too-many-branches
# ensure the vector is double
vector = vector.astype("float64")

if vector.ndim == 2:
vector = vector.flatten()

# create the circuit
nqbit = int(np.log2(len(vector)))
self.vector_circuit = QuantumCircuit(nqbit, name="Ub")
Expand Down
42 changes: 23 additions & 19 deletions vqls_prototype/tomography/htree_qst.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def get_tree(self):
"""Compute the tree"""

def init_tree():
"""_summary_
"""initialize the tree
Returns:
_type_: _description_
tree: tree
"""
trees = []
level_root, level_leaf = [], []
Expand All @@ -64,10 +64,10 @@ def init_tree():
return trees, level_root, level_leaf

def link_trees(trees):
"""_summary_
"""link multiple tress
Args:
trees (_type_): _description_
trees (list): list of trees
"""
ntree = len(trees)
level_root, level_leaf = [], []
Expand Down Expand Up @@ -95,7 +95,7 @@ def link_trees(trees):
return tree

def get_path(self):
"""_summary_"""
"""Create the paths between the root and all the leaves"""
paths = []
for inode in range(self.size):
paths.append(list(self.tree.rsearch(inode)))
Expand All @@ -105,7 +105,7 @@ def get_path_sparse_matrix(self):
"""transforms the path into a sparse matrix
Returns:
_type_: _description_
coo matrix: sparse matrix of the path
"""
row_idx, col_idx, vals = [], [], []
for ip, path in enumerate(self.path_to_node):
Expand All @@ -118,10 +118,10 @@ def get_path_sparse_matrix(self):
)

def get_circuits(self):
"""_summary_
"""Create the circuits containing a single H on a given qubit after the circuit
Args:
circuits (_type_): _description_
circuits (list): List of circuits
"""
list_circuits = [self.circuit.measure_all(inplace=False)]

Expand All @@ -133,10 +133,10 @@ def get_circuits(self):
return list_circuits

def get_samples(self, parameters):
"""_summary_
"""Sample the circuit
Args:
sampler (_type_): _description_
parameters (np.array): values of the variational parameters of the circuit
"""
results = (
self.sampler.run(self.list_circuits, [parameters] * self.ncircuits)
Expand All @@ -152,10 +152,10 @@ def get_samples(self, parameters):
return samples

def get_weight(self, samples):
"""_summary_
"""Get the relative sign between parent/child node
Args:
samples (_type_): _description_
samples (list): lit of samples of circuits
"""
# root
weights = np.zeros_like(samples[0])
Expand All @@ -173,10 +173,10 @@ def get_weight(self, samples):
return weights

def get_signs(self, weights):
"""Agregate the signs of the statevector
"""Compute the signs of each components
Args:
weights (np.array):
weights (np.array): relative sign between parent/child in the tree
"""

# if the path is not known
Expand All @@ -191,23 +191,27 @@ def get_signs(self, weights):
return np.multiply.reduceat(mat.data, self.idx_path_matrix[:-1])

def get_relative_amplitude_sign(self, parameters):
"""_summary_
"""Get the relative amplitude of each components relative to the root
Args:
parameters (_type_): _description_
parameters (np.array): values of the variational parameters of the circuit
"""
samples = self.get_samples(parameters)
weights = self.get_weight(samples)
return self.get_signs(weights)

def get_statevector(self, parameters):
"""_summary_
"""Get the statevector of the circuit
Args:
parameters (_type_): _description_
parameters (np.array): values of the variational parameters of the circuit
"""
samples = self.get_samples(parameters)
amplitudes = np.sqrt(samples[0])
if np.any(samples[0] < 0):
print("Warning : Negative sampling values found in HTree")
amplitudes = np.sqrt(np.abs(samples[0]))
else:
amplitudes = np.sqrt(samples[0])
weights = self.get_weight(samples)
signs = self.get_signs(weights)
return amplitudes * signs

0 comments on commit 42c5558

Please sign in to comment.