from qiskit_ibm_runtime import QiskitRuntimeService
# Save an IBM Quantum account.
QiskitRuntimeService.save_account(channel='ibm_quantum', 
                                  #channel='ibm_cloud', 
                                  token='17efde49764005e8eeb00dd065d44bc208778be72d44b475e508d20504818786f842988b0e506515c78debdd1b0c4b570717863db5e4f85569fb43c4c8626b8a',
                                  overwrite=True)
service = QiskitRuntimeService(
    channel='ibm_quantum',
    instance='ibm-q/open/main'
    #instance='ibm-q-research-2/federal-uni-sant-1/main'
)

program_inputs = {'iterations': 1}
options = {"backend_name": "ibmq_qasm_simulator"}
job = service.run(program_id="hello-world",
                  options=options,
                  inputs=program_inputs
                 )
#print(f"job id: {job.job_id()}")
result = job.result()
print(result)

backend = service.get_backend("ibmq_qasm_simulator")

from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit.primitives import Estimator
from qiskit import QuantumCircuit

#circuit = random_circuit(2, 2, seed=1).decompose(reps=1)
circuit = QuantumCircuit(2)
circuit.x(0)
circuit.draw(output='mpl')
observable = SparsePauliOp("IZ") # ordem ...210

#options = {"backend_name": "ibmq_qasm_simulator"}
estimator = Estimator()#options=options)
job = estimator.run(circuit, observable)
result = job.result()

display(circuit.draw("mpl"))
print(f" > Observable: {observable.paulis}")
print(f" > Expectation value: {result.values[0]}")
print(f" > Metadata: {result.metadata[0]}")

import math
qc1 = QuantumCircuit(2); qc2 = QuantumCircuit(2)
qc1.h(1)
qc2.h(0); qc2.p(-math.pi/2, 0)

circuits = (
    #random_circuit(2, 2, seed=0).decompose(reps=1),
    #random_circuit(2, 2, seed=1).decompose(reps=1),
    qc1, qc2
)
observables = (
    SparsePauliOp("XZ"),
    SparsePauliOp("IY"),
)

estimator = Estimator()
job = estimator.run(circuits, observables)
result = job.result()

[display(cir.draw("mpl")) for cir in circuits]
print(f" > Observables: {[obs.paulis for obs in observables]}")
print(f" > Expectation values: {result.values.tolist()}")
print(f" > Metadata: {result.metadata}")

circuits = (
    random_circuit(2, 2, seed=0).decompose(reps=1),
    random_circuit(2, 2, seed=1).decompose(reps=1),
)
observables = (
    SparsePauliOp("XZ"),
    SparsePauliOp("IY"),
)

estimator = Estimator()
job_0 = estimator.run(circuits[0], observables[0])
job_1 = estimator.run(circuits[1], observables[1])
result_0 = job_0.result()
result_1 = job_1.result()

[display(cir.draw("mpl")) for cir in circuits]
print(f" > Observables: {[obs.paulis for obs in observables]}")
print(f" > Expectation values [0]: {result_0.values.tolist()[0]}")
print(f" > Metadata [0]: {result_0.metadata[0]}")
print(f" > Expectation values [1]: {result_1.values.tolist()[0]}")
print(f" > Metadata [1]: {result_1.metadata[0]}")

from qiskit.circuit.library import RealAmplitudes

circuit = RealAmplitudes(num_qubits=2, reps=2).decompose(reps=1)
observable = SparsePauliOp("ZI")
parameter_values = [0, 1, 2, 3, 4, 5]

estimator = Estimator()
job = estimator.run(circuit, observable, parameter_values)
result = job.result()

display(circuit.draw("mpl"))
print(f" > Observable: {observable.paulis}")
print(f" > Parameter values: {parameter_values}")
print(f" > Expectation value: {result.values}")
print(f" > Metadata: {result.metadata[0]}")

circuit = RealAmplitudes(num_qubits=2, reps=1).decompose(reps=1)
display(circuit.draw("mpl"))

from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Estimator, Options

circuit = random_circuit(2, 2, seed=1).decompose(reps=1)
observable = SparsePauliOp("IY")

options = Options()
options.optimization_level = 2
options.resilience_level = 2

service = QiskitRuntimeService()
with Session(service=service, backend="ibmq_qasm_simulator") as session:
    estimator = Estimator(session=session, options=options)
    job = estimator.run(circuit, observable)

result = job.result()

display(circuit.draw("mpl"))
print(f" > Observable: {observable.paulis}")
print(f" > Expectation value: {result.values[0]}")
print(f" > Metadata: {result.metadata[0]}")

from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Estimator, Options

circuit = random_circuit(2, 2, seed=1).decompose(reps=1)
observable = SparsePauliOp("IY")

options = Options()
options.optimization_level = 2
options.resilience_level = 2

service = QiskitRuntimeService()
with Session(service=service, backend="ibmq_belem") as session:
    estimator = Estimator(session=session, options=options)
    job = estimator.run(circuit, observable)

result = job.result()

display(circuit.draw("mpl"))
print(f" > Observable: {observable.paulis}")
print(f" > Expectation value: {result.values[0]}")
print(f" > Metadata: {result.metadata[0]}")

from qiskit_ibm_runtime import Session, Options

circuit = random_circuit(2, 2, seed=1).decompose(reps=1)
observable = SparsePauliOp("IY")

options = Options()
options.optimization_level = 2
options.resilience_level = 2

service = QiskitRuntimeService()
backend = service.get_backend("ibmq_belem")
with Session(service=service, backend=backend):
    estimator = Estimator()
    job = estimator.run(circuit, observable)

result = job.result()

display(circuit.draw("mpl"))
print(f" > Observable: {observable.paulis}")
print(f" > Expectation value: {result.values[0]}")
print(f" > Metadata: {result.metadata[0]}")

from qiskit.circuit.random import random_circuit
from qiskit.primitives import Sampler

circuit = random_circuit(2, 2, seed=1).decompose(reps=1)
circuit.measure_all()

sampler = Sampler()
job = sampler.run(circuit)
result = job.result()

display(circuit.draw("mpl"))
print(f" > Quasi probability distribution: {result.quasi_dists[0]}")
#print(f" > Metadata: {result.metadata[0]}")
#print(result.quasi_dists,result.quasi_dists[0][1])
print(result.quasi_dists[0][0]+result.quasi_dists[0][1]+result.quasi_dists[0][2]+result.quasi_dists[0][3])

from qiskit.circuit.random import random_circuit
from qiskit.primitives import Sampler

circuits = (
random_circuit(2, 2, seed=0).decompose(reps=1),
random_circuit(2, 2, seed=1).decompose(reps=1),
)
[c.measure_all() for c in circuits]

sampler = Sampler()
job = sampler.run(circuits)
result = job.result()

[display(cir.draw("mpl")) for cir in circuits]
print(f" > Quasi probability distributions: {result.quasi_dists}")
#print(f" > Metadata: {result.metadata}")

from qiskit.circuit.library import RealAmplitudes
# RealAmplitudes is one way to generate a parametrized circuit
from qiskit.primitives import Sampler

circuit = RealAmplitudes(num_qubits=2, reps=2).decompose(reps=1)
circuit.measure_all()
parameter_values = [0, 1, 2, 3, 4, 5]

sampler = Sampler()
job = sampler.run(circuit, parameter_values)
result = job.result()

display(circuit.draw("mpl"))
print(f" > Parameter values: {parameter_values}")
print(f" > Quasi probability distribution: {result.quasi_dists[0]}")
print(f" > Metadata: {result.metadata[0]}")

from qiskit.circuit.random import random_circuit
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Sampler, Options
backend = service.get_backend("ibmq_qasm_simulator")

circuit = random_circuit(2, 2, seed=2).decompose(reps=1)
circuit.measure_all()

options = Options()
options.optimization_level = 2
options.resilience_level = 0

service = QiskitRuntimeService()
with Session(service=service, backend=backend):
    sampler = Sampler()
    job = sampler.run(circuit)

result = job.result()

display(circuit.draw("mpl"))
print(f" > Quasi probability distribution: {result.quasi_dists[0]}")
print(f" > Metadata: {result.metadata[0]}")

from qiskit.circuit.random import random_circuit
from qiskit_ibm_runtime import QiskitRuntimeService, Session, Sampler, Options
backend = service.get_backend("ibmq_quito")

circuit = random_circuit(2, 2, seed=2).decompose(reps=1)
circuit.measure_all()

options = Options()
options.optimization_level = 2
options.resilience_level = 0

service = QiskitRuntimeService()
with Session(service=service, backend=backend):
    sampler = Sampler()
    job = sampler.run(circuit)
    print(job.job_id())

result = job.result()

display(circuit.draw("mpl"))
print(f" > Quasi probability distribution: {result.quasi_dists[0]}")
print(f" > Metadata: {result.metadata[0]}")

