I want to write code for a custom Variational Quantum Eigensolver (VQE) capable of computing eigenvalue(s) for non-Hermitian systems, based on this paper. I have formulated a cost function (provided below, which may need correction), and I wish to test this custom VQE on a non-Hermitian matrix, for example, $$H = \begin{bmatrix}1 & -2 \\ 3 & 4 \end{bmatrix}\,.$$ I attempted to do this using VQE from qiskit_algorithm but encountered two issues. Firstly, it doesn't seem to allow the cost_function
as an input, which it used to do earlier, I believe. Secondly, I tried to convert the matrix into an Operator through SparsePauliOp, but that also doesn't seem to work. Though I used qiskit, I don't mind suggestions or guidance in other libraries.
def cost_function(params):
# Apply the ansatz
U = ansatz.assign_parameters(params)
# n is number of qubits (len(H))
# Compute the quantum process snapshot
QPS = qiskit.QuantumCircuit(n + 1, n)
QPS.h(0)
QPS.append(U, range(1, n + 1))
QPS.append(qiskit.quantum_info.Operator(A), range(1, n + 1))
QPS.append(U.inverse(), range(1, n + 1))
QPS.measure(range(1, n + 1), range(n))
# Execute the circuit
result = quantum_instance.execute(QPS).result()
counts = result.get_counts()
# Compute the expectation value
expectation = 0
for key, value in counts.items():
# Convert the key to a binary string
key = format(int(key), '0' + str(n) + 'b')
# Compute the probability
prob = value / shots
# Compute the phase
phase = 0
for i in range(n):
phase += int(key[i]) * 2 * np.pi / (2 ** n)
# Add the contribution to the expectation value
expectation += prob * np.exp(1j * phase)
# Return the absolute value of the expectation value
return numpy.abs(expectation)