from qiskit_ibm_runtime import QiskitRuntimeService
 
# Save an IBM Quantum account and set it as your default account.
QiskitRuntimeService.save_account(channel="ibm_quantum", token='<YOUR_TOKEN>', overwrite=True,set_as_default=True)
 
# Load saved credentials
service = QiskitRuntimeService()

service.backend()
!pip install qiskit[visualization]

# Imports for Qiskit
from qiskit import QuantumCircuit, execute,IBMQ
from qiskit.compiler import transpile, assemble
from qiskit import QuantumCircuit, transpile, IBMQ, execute #i added to remove error
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from qiskit import *
from qiskit import IBMQ
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.visualization import plot_histogram
from copy import deepcopy
from matplotlib import pyplot as plt
from qiskit import *
from qiskit.tools.visualization import *
from qiskit.visualization import plot_histogram, plot_bloch_vector
from math import sqrt, pi

from qiskit import QuantumCircuit, assemble
from math import pi, sqrt
from qiskit.visualization import plot_bloch_multivector, plot_histogram
#sim = Aer.get_backend('aer_simulator')
from IPython.display import clear_output
#Quantum Volume
from IPython.display import clear_output

#Import Qiskit classes
import qiskit
from qiskit import assemble, transpile
#from qiskit.providers.aer.noise import NoiseModel
#from qiskit.providers.aer.noise.errors.standard_errors import depolarizing_error, thermal_relaxation_error

#Import the qv function
#import qiskit.ignis.verification.quantum_volume as qv
#Import qv measurement classes
#from qiskit_experiments.framework import BatchExperiment
#from qiskit_experiments.library import QuantumVolume
#from qiskit import Aer
#from qiskit.providers.aer import AerSimulator

from qiskit.circuit.random import random_circuit
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.tools.monitor import job_monitor

# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
#from ibm_quantum_widgets import *
#from qiskit.providers.aer import QasmSimulator
from qiskit import *
import numpy as np

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit.barrier()
circuit.x(input_);
#---------------Psi 1--------------------------
circuit.barrier();
circuit.h(clock[0]);
circuit.h(clock[1]);
#--------------Psi 2---------------------------
circuit.barrier();
# e^{i*A*t}
circuit.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit.barrier();     
circuit.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit.h(clock[0]);
circuit.swap(clock[0], clock[1]);
#--------------Psi 4---------------------------
circuit.barrier();
circuit.cry(np.pi, clock[0], ancilla);
circuit.cry(np.pi/3, clock[1], ancilla);
#--------------Psi 5---------------------------
circuit.barrier();
circuit.swap(clock[0], clock[1]);
circuit.h(clock[0]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit.cp(np.pi/float(2**(k-j)), clock[k], clock[j]);
circuit.h(clock[1])
#--------------Psi 6---------------------------
circuit.barrier();
circuit.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');
# e^{i*A*t}
circuit.cu(np.pi/2, np.pi/2, -np.pi/2, -3*np.pi/4, clock[0], input_, label='U');
#--------------Psi 7--------------------------
circuit.barrier();
circuit.h(clock[0]);
circuit.h(clock[1]);
#--------------Psi 8---------------------------
circuit.barrier();

circuit.draw(output='mpl', style = 'iqp')

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit0 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit0.barrier()
circuit0.x(input_);
circuit0.measure_all()
circuit0.draw(output='mpl',idle_wires=False)

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt0 = transpile(circuit0, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt0.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt0
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data = binary_prob

# Extract all possible combinations of 4-bit binary headings
all_combinations = [format(i, '04b') for i in range(16)]

# Iterate over the data and fill in missing values with zeros
filled_data = []
for d in data:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data.append(filled_row)

# Write the filled data to a CSV file
csv_file = "data.csv"
with open(csv_file, mode='w', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    writer.writeheader()
    for row in filled_data:
        writer.writerow(row)

print(f"Data has been written to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit1 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit1.barrier()
circuit1.x(input_);
#---------------Psi 1--------------------------
circuit1.barrier();
circuit1.h(clock[0]);
circuit1.h(clock[1]);
circuit1.measure_all()
circuit1.draw(output='mpl',idle_wires=False)
#--------------Psi 2---------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt1 = transpile(circuit1, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt1.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt1
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit2 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit2.barrier()
circuit2.x(input_);
#---------------Psi 1--------------------------
circuit2.barrier();
circuit2.h(clock[0]);
circuit2.h(clock[1]);
#--------------Psi 2---------------------------
circuit2.barrier();
# e^{i*A*t}
circuit2.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit2.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');
circuit2.measure_all()
circuit2.draw(output='mpl',idle_wires=False)
#--------------Psi 3---------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt2 = transpile(circuit2, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt2.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt2
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit3 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit3.barrier()
circuit3.x(input_);
#---------------Psi 1--------------------------
circuit3.barrier();
circuit3.h(clock[0]);
circuit3.h(clock[1]);
#--------------Psi 2---------------------------
circuit3.barrier();
# e^{i*A*t}
circuit3.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit3.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit3.barrier();     
circuit3.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit3.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit3.h(clock[0]);
circuit3.swap(clock[0], clock[1]);
circuit3.measure_all();
circuit3.draw(output='mpl',idle_wires=False)

#--------------Psi 4---------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt3 = transpile(circuit3, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt3.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt3
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit4 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit4.barrier()
circuit4.x(input_);
#---------------Psi 1--------------------------
circuit4.barrier();
circuit4.h(clock[0]);
circuit4.h(clock[1]);
#--------------Psi 2---------------------------
circuit4.barrier();
# e^{i*A*t}
circuit4.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit4.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit4.barrier();     
circuit4.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit4.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit4.h(clock[0]);
circuit4.swap(clock[0], clock[1]);
#--------------Psi 4---------------------------
circuit4.barrier();
circuit4.cry(np.pi, clock[0], ancilla);
circuit4.cry(np.pi/3, clock[1], ancilla);
circuit4.measure_all()
circuit4.draw(output='mpl',idle_wires=False)
#--------------Psi 5---------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt4 = transpile(circuit4, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt4.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt4
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit5 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit5.barrier()
circuit5.x(input_);
#---------------Psi 1--------------------------
circuit5.barrier();
circuit5.h(clock[0]);
circuit5.h(clock[1]);
#--------------Psi 2---------------------------
circuit5.barrier();
# e^{i*A*t}
circuit5.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit5.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit5.barrier();     
circuit5.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit5.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit5.h(clock[0]);
circuit5.swap(clock[0], clock[1]);
#--------------Psi 4---------------------------
circuit5.barrier();
circuit5.cry(np.pi, clock[0], ancilla);
circuit5.cry(np.pi/3, clock[1], ancilla);
#--------------Psi 5---------------------------
circuit5.barrier();
circuit5.swap(clock[0], clock[1]);
circuit5.h(clock[0]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit5.cp(np.pi/float(2**(k-j)), clock[k], clock[j]);
circuit5.h(clock[1])
circuit5.measure_all()
circuit5.draw(output='mpl',idle_wires=False)
#--------------Psi 6---------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt5 = transpile(circuit5, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt5.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt5
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit6 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit6.barrier()
circuit6.x(input_);
#---------------Psi 1--------------------------
circuit6.barrier();
circuit6.h(clock[0]);
circuit6.h(clock[1]);
#--------------Psi 2---------------------------
circuit6.barrier();
# e^{i*A*t}
circuit6.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit6.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit6.barrier();     
circuit6.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit6.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit6.h(clock[0]);
circuit6.swap(clock[0], clock[1]);
#--------------Psi 4---------------------------
circuit6.barrier();
circuit6.cry(np.pi, clock[0], ancilla);
circuit6.cry(np.pi/3, clock[1], ancilla);
#--------------Psi 5---------------------------
circuit6.barrier();
circuit6.swap(clock[0], clock[1]);
circuit6.h(clock[0]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit6.cp(np.pi/float(2**(k-j)), clock[k], clock[j]);
circuit6.h(clock[1])
#--------------Psi 6---------------------------
circuit6.barrier();
circuit6.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');
# e^{i*A*t}
circuit6.cu(np.pi/2, np.pi/2, -np.pi/2, -3*np.pi/4, clock[0], input_, label='U');
circuit6.measure_all()
circuit6.draw(output='mpl',idle_wires=False)
#--------------Psi 7--------------------------

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt6 = transpile(circuit6, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt6.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt6
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")

# Register declaration
clock = QuantumRegister(2, name='clock')
input_ = QuantumRegister(1, name='b')
ancilla = QuantumRegister(1, name='ancilla')
circuit7 = QuantumCircuit(ancilla, clock, input_)
#---------------Psi 0--------------------------
circuit7.barrier()
circuit7.x(input_);
#---------------Psi 1--------------------------
circuit7.barrier();
circuit7.h(clock[0]);
circuit7.h(clock[1]);
#--------------Psi 2---------------------------
circuit7.barrier();
# e^{i*A*t}
circuit7.cu(np.pi/2, -np.pi/2, np.pi/2, 3*np.pi/4, clock[0], input_, label='U');
    
# e^{i*A*t*2}
circuit7.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');

#--------------Psi 3---------------------------
circuit7.barrier();     
circuit7.h(clock[1]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit7.cp(-np.pi/float(2**(k-j)), clock[k], clock[j]); #cu1 changes to cp in the new version
circuit7.h(clock[0]);
circuit7.swap(clock[0], clock[1]);
#--------------Psi 4---------------------------
circuit7.barrier();
circuit7.cry(np.pi, clock[0], ancilla);
circuit7.cry(np.pi/3, clock[1], ancilla);
#--------------Psi 5---------------------------
circuit7.barrier();
circuit7.swap(clock[0], clock[1]);
circuit7.h(clock[0]);
for j in reversed(range(2)):
    for k in reversed(range(j+1,2)):
        circuit7.cp(np.pi/float(2**(k-j)), clock[k], clock[j]);
circuit7.h(clock[1])
#--------------Psi 6---------------------------
circuit7.barrier();
circuit7.cu(np.pi, np.pi, 0, 0, clock[1], input_, label='U2');
# e^{i*A*t}
circuit7.cu(np.pi/2, np.pi/2, -np.pi/2, -3*np.pi/4, clock[0], input_, label='U');
#--------------Psi 7--------------------------
circuit7.barrier();
circuit7.h(clock[0]);
circuit7.h(clock[1]);
#--------------Psi 8---------------------------
circuit7.measure_all();
circuit.draw(output='mpl', style = 'iqp')

from qiskit_ibm_provider import IBMProvider
provider = IBMProvider()
transpiled_ckt7 = transpile(circuit7, provider.get_backend('ibm_brisbane'), seed_transpiler = 13,optimization_level=0) 
transpiled_ckt7.draw(output = 'mpl',idle_wires=False)

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Options
 
service = QiskitRuntimeService()
options = Options()
options.resilience_level = 0
options.optimization_level = 0
#backend = service.least_busy(operational=True, simulator=False)
 
example_circuit = transpiled_ckt7
service = QiskitRuntimeService()
backend = service.backend("ibm_brisbane")
sampler = Sampler(backend, options=options)
job = sampler.run(example_circuit)
print(f"job id: {job.job_id()}")
result = job.result()
print(result)

binary_prob = [quasi_dist.binary_probabilities() for quasi_dist in result.quasi_dists]
plot_histogram(binary_prob)

import csv

data1 = binary_prob

# Fill in missing values in data1 with zeros
filled_data1 = []
for d in data1:
    filled_row = {key: d.get(key, 0) for key in all_combinations}
    filled_data1.append(filled_row)

# Append filled_data1 to filled_data
filled_data.extend(filled_data1)

# Write the updated filled_data to the same CSV file
csv_file = "data.csv"
with open(csv_file, mode='a', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=all_combinations)
    for row in filled_data1:
        writer.writerow(row)

print(f"Data1 has been added to {csv_file}")