<jupyter_start><jupyter_text>Script performing an example variational algorithm using Qiskit Aqua<jupyter_code># =======
# imports
# =======

from qiskit.aqua import get_aer_backend

from qiskit.aqua.components.initial_states import Zero
from qiskit.aqua.components.variational_forms import RY
from qiskit.aqua.operator import Operator
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.aqua.algorithms import VQE
from qiskit.aqua import QuantumInstance
import warnings
warnings.filterwarnings('ignore')
# =========
# constants
# =========

# number of qubits
num_qubits = 2

# pauli operators for a hamiltonian
pauli_dict = {
            'paulis': [{"coeff": {"imag": 0.0, "real": -1.052373245772859}, "label": "II"},
                       {"coeff": {"imag": 0.0, "real": 0.39793742484318045}, "label": "IZ"},
                       {"coeff": {"imag": 0.0, "real": -0.39793742484318045}, "label": "ZI"},
                       {"coeff": {"imag": 0.0, "real": -0.01128010425623538}, "label": "ZZ"},
                       {"coeff": {"imag": 0.0, "real": 0.18093119978423156}, "label": "XX"}
                       ]
    }
# ======================
# setting up the circuit
# ======================

# define the initial state
init_state = Zero(num_qubits)

# get a variational ansatz
ansatz = RY(num_qubits)

# operator from hamiltonian
qubit_op = Operator.load_from_dict(pauli_dict)

# get an optimizer
optimizer = COBYLA(maxiter=1000, disp=True)

# form the algorithm
vqe = VQE(qubit_op, ansatz, optimizer)

# get a backend
backend = get_aer_backend("statevector_simulator")

# get a quantum instance
qinstance = QuantumInstance(backend, shots=1024)
# ===================
# do the optimization
# ===================

result = vqe.run(qinstance)
# ================
# show the results
# ================

# output of the optimization
print(result)

# show the circuit
circuit = vqe.construct_circuit(list(range(8)), backend)[0]
print(circuit)<jupyter_output>{'num_optimizer_evals': 119, 'min_val': -1.8572750288166913, 'opt_params': array([-0.43815468,  3.93891868, -1.16822459,  0.66026905, -3.14394187,
        0.11488149,  0.87200262, -1.6562297 ]), 'eval_time': 3.454651355743408, 'eval_count': 119, 'energy': -1.8572750288166913, 'eigvals': array([-1.85727503]), 'min_vector': array([-1.18765769e-05+0.j,  9.93762018e-01+0.j, -1.11521527e-01+0.j,
       -3.16398338e-05+0.j]), 'eigvecs': array([[-1.18765769e-05+0.j,  9.93762018e-01+0.j, -1.11521527e-01+0.j,
        -3.16398338e-05+0.j]])}
        ┌───────────┐ ░                               ░ ┌───────────┐ ░ »
q_0: |0>┤ U3(0,0,0) ├─░───────────────■───────────────░─┤ U3(2,0,0) ├─░─»
        ├───────────┤ ░ ┌──────────┐┌─┴─┐┌──────────┐ ░ ├───────────┤ ░ »
q_1: |0>┤ U3(1,0,0) ├─░─┤ U2(0,pi) ├┤ X ├┤ U2(0,pi) ├─░─┤ U3(3,0,0) ├─░─»
        └───────────┘ ░ └──────────┘└───┘└──────────┘ ░ └───────────┘ ░ »
«                                   ░ ┌───────────┐ ░                  »
«q_0: ─────────────[...]
{'fraction_non_alphanumeric': 0.26100100772589857, 'fraction_numerical': 0.12294255962378232, 'mean_word_length': 3.6386292834890965, 'pattern_counts': {'":': 20, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '629073', 'license': 'Apache-2.0', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1187, 'n_words': 233, 'path': 'qiskit-vqe-example.ipynb', 'repo_name': 'jessicapointing/qiskit-iqx-tutorials', 'size': '2977'}