When doing FixedAnsatzVQE in matrix mode, error is thrown
from variationalalgorithms.utils.fixed_ansatz_circuits import hardware_efficient_circuit
from variationalalgorithms.vqe import FixedAnsatzVQE
matrix = [[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 1.+0.j, -1.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 1.+0.j, 1.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, -1.+0.j, 1.+0.j,
0.+0.j, 0.+0.j, -1.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 1.+0.j, 1.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 1.+0.j, -1.+0.j,
0.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 1.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 0.+0.j,
0.+0.j, 0.+0.j],
[ 0.+0.j, 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1.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 1.+0.j]])
vqe = FixedAnsatzVQE(hardware_efficient_circuit(4, 'ryrz', 2), evaluation_matrix=matrix)
vqe.run()
The error occurs in line 129 ofvariational_quantum_eigensolver.py
:
def _run_full_circuits(self, variational_circuit):
"""
Run the full circuits
:param variational_circuit: Variational part of full circuit
:return: counts data (e.g. ['000':10,'010':31,'011':20,'110':40]
"""
full_circuits = self._full_circuits(variational_circuit)
num_shots = 1 if is_statevector_backend(self.backend) else SIMULATOR_SHOTS
result = {full_circuit.name: execute(full_circuit, self.backend, shots=num_shots).result()
for full_circuit in full_circuits}
return result
It's expecting full_circuits to be a list, but in fact it is just one circuit. When you iterate through one circuit, you start iterating through qubits rather than circuits, causing the error.
Suggested fix
Replace
def _full_circuits(self, variational_circuit):
"""
Create the circuit(s) that will be run. If matrix_mode, the circuit to run is just the variational_circuit.
If not matrix_mode, pauli rotations corresponding to the pauli operators specified in the hamiltonian are added
along with appropriate basis change operators
:param variational_circuit:
:return:
"""
if self.matrix_mode:
return variational_circuit
with
def _full_circuits(self, variational_circuit):
"""
Create the circuit(s) that will be run. If matrix_mode, the circuit to run is just the variational_circuit.
If not matrix_mode, pauli rotations corresponding to the pauli operators specified in the hamiltonian are added
along with appropriate basis change operators
:param variational_circuit:
:return:
"""
if self.matrix_mode:
return [variational_circuit]
Edited by Benjamin Jaderberg