Skip to content

Commit

Permalink
Merge pull request #190 from UCL-CCS/vqe_bugfix
Browse files Browse the repository at this point in the history
Bugfix in VQE_Driver
  • Loading branch information
TimWeaving authored Oct 9, 2024
2 parents 7684d90 + 0f42360 commit 810243e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
9 changes: 5 additions & 4 deletions symmer/evolution/variational_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from symmer.evolution import PauliwordOp_to_QuantumCircuit, get_CNOT_connectivity_graph, topology_match_score
from networkx.algorithms.cycles import cycle_basis
from scipy.optimize import minimize
from scipy.sparse import csc_array
from copy import deepcopy
import numpy as np
from typing import *
Expand Down Expand Up @@ -84,13 +85,13 @@ def get_state(self,
if self.expectation_eval == 'observable_rotation':
return list(zip(evolution_obj, -2*x))
else:
state = Statevector(evolution_obj.bind_parameters(x)).data
state = Statevector(evolution_obj.assign_parameters(x)).data.reshape([-1,1])
if self.expectation_eval == 'dense_array':
return state.to_matrix().reshape([-1,1])
return state
elif self.expectation_eval == 'sparse_array':
return state.to_spmatrix().reshape([-1,1])
return csc_array(state)
elif self.expectation_eval.find('symbolic') != -1:
return QuantumState.from_array(state.to_matrix().reshape([-1,1]))
return QuantumState.from_array(state)

def _f(self,
observable: PauliwordOp,
Expand Down
2 changes: 1 addition & 1 deletion symmer/process_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProcessHandler:
method = 'mp'
else:
method = 'ray'

verbose = False

def __init__(self):
Expand Down
37 changes: 19 additions & 18 deletions tests/test_approximate/test_approximate_tensor_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,35 @@ def coeff_vec_1():
def coeff_vec_2():
return np.random.random(4)

# UNSTABLE

############################################
# Testing different initialization methods #
############################################

def test_from_list(
pauli_list_1,
coeff_vec_1,
):
MPO = MPOOp(pauli_list_1, coeff_vec_1)
matrix_MPO = MPO.to_matrix
# def test_from_list(
# pauli_list_1,
# coeff_vec_1,
# ):
# MPO = MPOOp(pauli_list_1, coeff_vec_1)
# matrix_MPO = MPO.to_matrix

WordOp = PauliwordOp.from_list(pauli_list_1, coeff_vec_1)
matrix_WordOp = WordOp.to_sparse_matrix.toarray()
# WordOp = PauliwordOp.from_list(pauli_list_1, coeff_vec_1)
# matrix_WordOp = WordOp.to_sparse_matrix.toarray()

assert(np.allclose(matrix_MPO, matrix_WordOp))
# assert(np.allclose(matrix_MPO, matrix_WordOp))

def test_from_dictionary(
pauli_list_1,
coeff_vec_1):
pauli_dict = dict(zip(pauli_list_1, coeff_vec_1))
MPO = MPOOp.from_dictionary(pauli_dict)
matrix_MPO = MPO.to_matrix
# def test_from_dictionary(
# pauli_list_1,
# coeff_vec_1):
# pauli_dict = dict(zip(pauli_list_1, coeff_vec_1))
# MPO = MPOOp.from_dictionary(pauli_dict)
# matrix_MPO = MPO.to_matrix

WordOp = PauliwordOp.from_list(pauli_list_1, coeff_vec_1)
matrix_WordOp = WordOp.to_sparse_matrix.toarray()
# WordOp = PauliwordOp.from_list(pauli_list_1, coeff_vec_1)
# matrix_WordOp = WordOp.to_sparse_matrix.toarray()

assert(np.allclose(matrix_MPO, matrix_WordOp))
# assert(np.allclose(matrix_MPO, matrix_WordOp))

############################################
# Testing QUIMB dmrg sovler #
Expand Down

0 comments on commit 810243e

Please sign in to comment.