%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
# Loading your IBM Q account(s)
provider = IBMQ.load_account()

from qiskit.aqua.operators import OperatorBase, ListOp, PrimitiveOp, PauliOp
from qiskit.quantum_info import Pauli
from qiskit.aqua.algorithms import VQE, NumPyEigensolver
from qiskit.circuit.library import TwoLocal
from qiskit.aqua.components.optimizers import COBYLA, SPSA, SLSQP
import numpy as np

ansatz = TwoLocal(num_qubits=2,
                  rotation_blocks=['rx','rz'],
                  entanglement_blocks='cx',
                  entanglement='full',
                  reps=1)
ansatz.draw()

optimizer = SLSQP(maxiter=100)

Op = PauliOp(Pauli(label='II'), 0.5) - PauliOp(Pauli(label='XX'), 0.5) - 0.5*PauliOp(Pauli(label='YY')) + 0.5*PauliOp(Pauli(label='ZZ'))

# Op = PrimitiveOp(primitive=[[1,0],[0,1]], coeff=2.)
# xx = Pauli(label='XX')
# yy = Pauli(label='YY')
# zz = Pauli(label='ZZ')
# ii = Pauli(label='II')
# Op = ListOp([zz,yy,xx, ii], coeff=[0.5,-0.5,-0.5,0.5])
vqe = VQE(Op, ansatz, optimizer)

backend = Aer.get_backend("statevector_simulator")
vqe_result = np.real(vqe.run(backend)['eigenvalue'] )
vqe_result

np.min(np.linalg.eig(Op.to_matrix())[0])

Op.to_matrix()

