<jupyter_start><jupyter_text>Gray code encoding examples The data accompanying our paper was generated by the `run_qiskit_experiment.py` file in the main directory - but that is intended for batch jobs with multiple trials. In this notebook are a series of simple examples that demonstrate how to set up our Hamiltonian and run the VQE using the SPSA optimizer.<jupyter_code>import numpy as np
np.warnings.filterwarnings('ignore')

from noisyopt import minimizeSPSA

from qiskit import Aer

import sys
sys.path.append("../src/")
from hamiltonian import *
from utils import *
from qiskit_circuits import *
from qiskit_experiment import compute_energy

np.set_printoptions(precision=6, suppress=True)<jupyter_output><empty_output><jupyter_text>Statevector simulator examples  Gray code encoding<jupyter_code># Select backend
backend = Aer.get_backend('statevector_simulator')
# Number of states
N_states = 2

######################
# Gray code encoding #
######################
ham = DenseEncodingHamiltonian(N_states=N_states)

# Initialize guesses for parameters and run VQE
theta = np.random.uniform(low=-2*np.pi, high = 2*np.pi, size=(N_states-1, ))

res = minimizeSPSA(compute_energy,
        x0=theta,
        args=(backend, ham),
        niter=500,
        paired=False,
        a=0.628,
        c=0.1
)

print(res)<jupyter_output><empty_output><jupyter_text>One-hot encoding<jupyter_code># Select backend
backend = Aer.get_backend('statevector_simulator')

# Number of states
N_states = 4

####################
# One-hot encoding #
####################
ham = SparseEncodingHamiltonian(N_states=N_states)

# Initialize guesses for parameters and run VQE
theta = np.random.uniform(low=-2*np.pi, high = 2*np.pi, size=(N_states-1, ))

res = minimizeSPSA(compute_energy,
        x0=theta,
        args=(backend, ham),
        niter=500,
        paired=False,
        a=0.628,
        c=0.1
)

print(res)<jupyter_output><empty_output><jupyter_text>QASM simulator example<jupyter_code>backend = Aer.get_backend('qasm_simulator')

device = None # (No hardware device specified, no noise model)

N_states = 4

N_shots = 10000

# Choose a Hamiltonian
ham = DenseEncodingHamiltonian(N_states=N_states)
#ham = SparseEncodingHamiltonian(N_states=N_states)

# Initialize guesses for parameters and run VQE
theta = np.random.uniform(low=-2*np.pi, high = 2*np.pi, size=(N_states-1, ))

res = minimizeSPSA(compute_energy,
        x0=theta,
        args=(backend, ham, device, N_shots),
        niter=500,
        paired=False,
        a=0.628,
        c=0.1
)

print(res)<jupyter_output><empty_output><jupyter_text>Device noise example<jupyter_code>backend = Aer.get_backend('qasm_simulator')

# Device specifications are loaded separately
# Here, you can choose ibmq_essex, ibmq_vigo, ...
from device import Device
device_name = 'ibmq_essex'
device = Device(device_name)

N_states = 2

N_shots = 10000

# Choose a Hamiltonian
#$ham = DenseEncodingHamiltonian(N_states=N_states)
ham = SparseEncodingHamiltonian(N_states=N_states)
            
# Initialize guesses for parameters and run VQE
theta = np.random.uniform(low=-2*np.pi, high = 2*np.pi, size=(N_states-1, ))

res = minimizeSPSA(compute_energy,
        x0=theta,
        args=(backend, ham, device, N_shots),
        niter=500,
        paired=False,
        a=0.628,
        c=0.1
)

print(res)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12366548042704627, 'fraction_numerical': 0.019869513641755633, 'mean_word_length': 4.493485342019544, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '478889', 'license': 'MIT', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1116, 'n_words': 265, 'path': 'notebooks/Examples.ipynb', 'repo_name': 'pgysbers/GrayCode-QubitEncoding', 'size': '3372'}