1
$\begingroup$

when i try to call the VQE from qiskit I get an error of dimmismatch. The input matrix is of a 16x16 shape and I could not find out what where I introduce the error. In the picture The input matrix and the qubit0p is shown. I get the same error when trying to use QAOA as well

Thank you for taking the time.

             self.C = self.C[:,:len(self.C[0])]
                    self.B = self.B[:,:len(self.B[0])]
                    print("Input:",self.C)
                    qubitOp = MatrixOperator(self.C)                    
                    print(qubitOp) 
                    backend = Aer.get_backend('statevector_simulator')
                    var_form = RYRZ(qubitOp.num_qubits,10)
                    optim = COBYLA()
                    vqe = VQE(qubitOp, var_form, optim)
                # Runs the VQE over the backend defined above.
                    result_vqe = vqe.run(backend)

                    print(qubitOp) # prints operator properties 
                    print('energy', result_vqe['energy'], '\n')
                    print(result_vqe)

Here is the error

ValueError                                Traceback (most recent call last)
<ipython-input-1-03f1f6dbe796> in <module>
     76 print_state(state)
     77 
---> 78 bundle_adjust(state)
     79 print("= Bundle Adjusted State =")
     80 print_state(state)

<ipython-input-1-03f1f6dbe796> in bundle_adjust(state)
     39 
     40     # note: intervene here with other optimizers
---> 41     o.optimize()
     42 
     43     # normalize scale (note: ~ is __invert__, *noisy is __imul__)

~\Bachelor-Project\BA\optimizerVQE3.py in optimize(self)
     65     # run optimization
     66     def optimize(self):
---> 67         for event in self.optimize_it():
     68             pass
     69 

~\Bachelor-Project\BA\optimizerVQE3.py in optimize_it(self)
    131 
    132                 # Runs the VQE over the backend defined above.
--> 133                     result_vqe = vqe.run(backend)
    134 
    135                     print(qubitOp) # prints operator properties

~\Anaconda3\lib\site-packages\qiskit\aqua\algorithms\quantum_algorithm.py in run(self, quantum_instance, **kwargs)
     65                 quantum_instance.set_config(**kwargs)
     66             self._quantum_instance = quantum_instance
---> 67         return self._run()
     68 
     69     @abstractmethod

~\Anaconda3\lib\site-packages\qiskit\aqua\algorithms\adaptive\vqe\vqe.py in _run(self)
    314         if self._auto_conversion:
    315             self._operator = \
--> 316                 self._config_the_best_mode(self._operator, self._quantum_instance.backend)
    317             for i in range(len(self._aux_operators)):
    318                 if not self._aux_operators[i].is_empty():

~\Anaconda3\lib\site-packages\qiskit\aqua\algorithms\adaptive\vqe\vqe.py in _config_the_best_mode(self, operator, backend)
    236                                 "achieve the better performance. We convert "
    237                                 "the operator to weighted paulis.")
--> 238                     ret_op = op_converter.to_weighted_pauli_operator(operator)
    239         return ret_op
    240 

~\Anaconda3\lib\site-packages\qiskit\aqua\operators\op_converter.py in to_weighted_pauli_operator(operator)
     86                                list(itertools.product(possible_basis, repeat=num_qubits)),
     87                                task_kwargs={"matrix": operator._matrix},
---> 88                                num_processes=aqua_globals.num_processes)
     89         for trace_value, pauli in results:
     90             weight = trace_value * coeff

~\Anaconda3\lib\site-packages\qiskit\tools\parallel.py in parallel_map(task, values, task_args, task_kwargs, num_processes)
    142     results = []
    143     for _, value in enumerate(values):
--> 144         result = task(value, *task_args, **task_kwargs)
    145         results.append(result)
    146         _callback(0)

~\Anaconda3\lib\site-packages\qiskit\aqua\operators\op_converter.py in _conversion(basis, matrix)
     36 def _conversion(basis, matrix):
     37     pauli = Pauli.from_label(''.join(basis))
---> 38     trace_value = np.sum(matrix.dot(pauli.to_spmatrix()).diagonal())
     39     return trace_value, pauli
     40 

~\Anaconda3\lib\site-packages\scipy\sparse\base.py in dot(self, other)
    361 
    362         """
--> 363         return self * other
    364 
    365     def power(self, n, dtype=None):

~\Anaconda3\lib\site-packages\scipy\sparse\base.py in __mul__(self, other)
    478         if issparse(other):
    479             if self.shape[1] != other.shape[0]:
--> 480                 raise ValueError('dimension mismatch')
    481             return self._mul_sparse_matrix(other)
    482 

ValueError: dimension mismatch

enter image description here

$\endgroup$
0
$\begingroup$

The matrix you are printing seems to have 30 entries in each. When converting to Paulis form to run it expects the dimension to be a power of 2.

This code below (based on yours) does run when given a 16x16.

import numpy as np
from qiskit import Aer
from qiskit.aqua.algorithms import VQE
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.aqua.components.variational_forms import RYRZ
from qiskit.aqua.operators import MatrixOperator

np.random.seed = 75
matrix = np.random.rand(16, 16)
qubitOp = MatrixOperator(matrix)                    
print(qubitOp) 
backend = Aer.get_backend('statevector_simulator')
var_form = RYRZ(qubitOp.num_qubits,10)
optim = COBYLA()
vqe = VQE(qubitOp, var_form, optim)
result_vqe = vqe.run(backend)

print(qubitOp) # prints operator properties 
print('energy', result_vqe['energy'], '\n')
print(result_vqe)

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.