from qiskit.circuit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram

def decimal_to_binary(decimal):
    binary = ""
    if decimal == 0:
        return "0"
    while decimal > 0:
        binary = str(decimal % 2) + binary
        decimal //= 2
    return binary

# Example usage
decimal_number = 42
binary_number = decimal_to_binary(decimal_number)
print(f"The binary representation of {decimal_number} is: {binary_number}")


def binary_to_decimal(binary):
    decimal = 0
    power = len(binary) - 1
    for digit in binary:
        decimal += int(digit) * (2 ** power)
        power -= 1
    return decimal

# Example usage
binary_number = "101010"
decimal_number = binary_to_decimal(binary_number)
print(f"The decimal representation of {binary_number} is: {decimal_number}")


qcirc = QuantumCircuit(8)

qcirc.measure_all()

qcirc.draw()

qcirc.draw('mpl')

from qiskit.primitives import Sampler, Estimator, BackendEstimator, BackendSampler

backend = AerSimulator()  # decide the backend on which to run the circuit

sampler = BackendSampler(backend)  # create a sampler object
sampler2 = Sampler()

result = sampler.run(qcirc).result()          # run the circuit and save the results

result

print("Counts",result.quasi_dists[0])

plot_histogram(result.quasi_dists[0])

qcirc = QuantumCircuit(1)  # only a single qubit quantum circuit
qcirc.draw('mpl')          # let's visualize the circuit

qcirc.initialize([1,0])
qcirc.draw('mpl')

qcirc.measure_all()         # applying the measurement

qcirc.draw('mpl')

result = sampler.run(qcirc).result()
counts = result.quasi_dists[0]
counts

plot_histogram(counts)

result = sampler.run(qcirc, shots = 2000).result()
counts = result.quasi_dists[0]
counts

plot_histogram(counts)

from qiskit import QuantumRegister, ClassicalRegister

qr = QuantumRegister(1,'q0')            # you can specifically name the qubit q0
cr = ClassicalRegister(1,'c0')
qc = QuantumCircuit(qr, cr)             # combine the quantum and classical register
qc.initialize([1,0],0)
qc.measure(0,0)                         # apply the measurement as operator
qc.draw('mpl')

result = sampler.run(qc, shots = 2000).result()
counts = result.quasi_dists[0]
counts

plot_histogram(counts)