# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile
from qiskit.visualization import *
from ibm_quantum_widgets import *

# qiskit-ibmq-provider has been deprecated.
# Please see the Migration Guides in https://ibm.biz/provider_migration_guide for more detail.
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Estimator, Session, Options

# Loading your IBM Quantum account(s)
service = QiskitRuntimeService(channel="ibm_quantum")

# Invoke a primitive. For more details see https://docs.quantum.ibm.com/run/primitives
# result = Sampler().run(circuits).result()

from qiskit import QuantumCircuit, ClassicalRegister, transpile
from qiskit.providers.fake_provider import GenericBackendV2
 
# Create a circuit with classical control
creg = ClassicalRegister(19)
qc = QuantumCircuit(25)
qc.add_register(creg)
qc.h(0)
for i in range(18):
    qc.cx(0, i + 1)
for i in range(18):
    qc.measure(i, creg[i])
with qc.if_test((creg, 0)):
    qc.ecr(20, 21)
 
# Define backend with custom basis gates and 
# control flow instructions
backend = GenericBackendV2(
    num_qubits=25,
    basis_gates=["ecr", "id", "rz", "sx", "x"],
    control_flow=True,
  )
 
#Transpile
transpiled_qc = transpile(qc, backend)

job = backend.run(transpiled_qc)
#job = execute(qc, simulator, shots=1024)
# Wait for the job to finish
job_result = job.result()
# Get counts from the result
counts = job.result()[0].data.meas.get_counts()
#counts = job_result.get_counts()
"""# Plot the histogram
plot_histogram(counts)"""

