<jupyter_start><jupyter_text>Task4<jupyter_code>from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, execute, Aer
import pennylane as qml
import numpy as np
from numpy import pi
simulator = Aer.get_backend('qasm_simulator')<jupyter_output><empty_output><jupyter_text>Matrix and Decomposition<jupyter_code>M= np.matrix('1 0 0 0 ;0 0 -1 0;0 -1 0 0;0 0 0 1')
coeffs, obs_list = qml.utils.decompose_hamiltonian(M)
H = qml.Hamiltonian(coeffs, obs_list)
print(H)<jupyter_output>(0.5) [I0 I1]
+ (-0.5) [X0 X1]
+ (-0.5) [Y0 Y1]
+ (0.5) [Z0 Z1]<jupyter_text>Ansatz<jupyter_code>def Operational_circuit(params):
    qc = QuantumCircuit(2)
    
    #qc.h(range(2))
    qc.rx(params[0],0)
    qc.rx(params[1],1)
    qc.ry(params[2],0)
    qc.ry(params[3],1)
    qc.cx(0,1)
    qc.ry(params[4],0)
    qc.ry(params[5],1)
    
    return qc
    
Operational_circuit_try = Operational_circuit([1,2,3,4,5,6])
Operational_circuit_try.draw()<jupyter_output><empty_output><jupyter_text>Measurments for different components of H  1. ZZ<jupyter_code>def measure_zz_circuit(given_circuit):
    zz_meas = given_circuit.copy()
    zz_meas.measure_all()
    return zz_meas

zz_meas = measure_zz_circuit(Operational_circuit_try)
zz_meas.draw()
def measure_zz(given_circuit, num_shots = 10000):

    zz_meas = measure_zz_circuit(given_circuit)
    
    result = execute(zz_meas, backend = simulator, shots = num_shots).result()
    counts = result.get_counts(zz_meas)

    if '00' not in counts:
        counts['00'] = 0
    if '01' not in counts:
        counts['01'] = 0
    if '10' not in counts:
        counts['10'] = 0
    if '11' not in counts:
        counts['11'] = 0 

    total_counts = counts['00'] + counts['11'] + counts['01'] + counts['10']
    zz = counts['00'] + counts['11'] - counts['01'] - counts['10']
    zz = zz / total_counts
    
    return zz
zz = measure_zz(Operational_circuit_try)
print("<ZZ> =", str(zz))<jupyter_output><ZZ> = 0.5868<jupyter_text>2. XX<jupyter_code>def measure_xx_circuit(given_circuit):
    xx_meas = given_circuit.copy()
    xx_meas.h(0)
    xx_meas.h(1)
    xx_meas.measure_all()

    return xx_meas
xx_meas = measure_xx_circuit(Operational_circuit_try)
xx_meas.draw()
def measure_xx(given_circuit, num_shots = 10000):
    
    xx_meas = measure_xx_circuit(given_circuit)
    
    result = execute(xx_meas, backend = simulator, shots = num_shots).result()
    counts = result.get_counts(xx_meas)

    if '00' not in counts:
        counts['00'] = 0
    if '01' not in counts:
        counts['01'] = 0
    if '10' not in counts:
        counts['10'] = 0
    if '11' not in counts:
        counts['11'] = 0

    total_counts = counts['00'] + counts['11'] + counts['01'] + counts['10']
    xx = counts['00'] + counts['11'] - counts['01'] - counts['10']
    xx = xx / total_counts
    
    return xx
xx = measure_xx(Operational_circuit_try)
print("<XX> =", str(xx))<jupyter_output><XX> = -0.1034<jupyter_text>3. YY<jupyter_code>def measure_yy_circuit(given_circuit):
    yy_meas = given_circuit.copy()
    yy_meas.rz(-pi/2,0)
    yy_meas.rz(-pi/2,1)

    yy_meas.h(0)
    yy_meas.h(1)
    yy_meas.measure_all()

    return yy_meas
yy_meas = measure_yy_circuit(Operational_circuit_try)
yy_meas.draw()
def measure_yy(given_circuit, num_shots = 10000):
    
    yy_meas = measure_yy_circuit(given_circuit)
    
    result = execute(yy_meas, backend = simulator, shots = num_shots).result()
    counts = result.get_counts(yy_meas)

    if '00' not in counts:
        counts['00'] = 0
    if '01' not in counts:
        counts['01'] = 0
    if '10' not in counts:
        counts['10'] = 0
    if '11' not in counts:
        counts['11'] = 0

    total_counts = counts['00'] + counts['11'] + counts['01'] + counts['10']
    yy = counts['00'] + counts['11'] - counts['01'] - counts['10']
    yy = yy / total_counts
    
    return yy
yy = measure_yy(Operational_circuit_try)
print("<yy> =", str(yy))<jupyter_output><yy> = -0.0234<jupyter_text>Cost_Function<jupyter_code>def eigen_value(params):
    num_shots = 10000
    zz = measure_zz(Operational_circuit(params), num_shots = num_shots)
    xx = measure_xx(Operational_circuit(params), num_shots = num_shots)
    yy = measure_yy(Operational_circuit(params), num_shots = num_shots)
    
    ev = (0.5)*1 + (0.5)*zz + (-0.5)*xx+(-0.5)*yy
    
    return ev
params = [0,0,0,0,0,0]
ev = eigen_value(params)     #Try energy with params = [0,0,0,0,0,0]
print("The Eigen_Value of the trial state is", str(ev))<jupyter_output>The Eigen_Value of the trial state is 1.0070999999999999<jupyter_text>Finding Lowest Eignevalue<jupyter_code>from qiskit.aqua.components.optimizers import COBYLA,SLSQP,SPSA

optimizer = COBYLA(50,0.001)
ret = optimizer.optimize(num_vars=6, objective_function=eigen_value, initial_point=params)
eigen_value(ret[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1425925925925926, 'fraction_numerical': 0.05246913580246913, 'mean_word_length': 4.016511867905057, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '431198', 'license': 'Apache-2.0', 'n_tokens_mistral': 2093, 'n_tokens_neox': 1869, 'n_words': 360, 'path': 'Task4/Task4.ipynb', 'repo_name': 'CodieKev/QOSF_Tasks', 'size': '4860'}