from qiskit import QuantumCircuit, Aer, execute
import matplotlib.pyplot as plt

# Function to generate a random bit using a quantum circuit
def generate_random_bit():
    # Create a quantum circuit with one qubit and one classical bit
    qc = QuantumCircuit(1, 1)

    # Apply Hadamard gate to put the qubit in superposition
    qc.h(0)

    # Measure the qubit
    qc.measure(0, 0)

    # Use Aer's qasm_simulator
    simulator = Aer.get_backend('qasm_simulator')

    # Execute the circuit on the qasm simulator
    result = execute(qc, simulator, shots=1).result()

    # Get the measurement result
    counts = result.get_counts(qc)
    bit = int(list(counts.keys())[0])
    
    return bit

# Generate a random bit
random_bit = generate_random_bit()
print(f"Random bit: {random_bit}")

# Generate multiple random bits
num_bits = 10
random_bits = [generate_random_bit() for _ in range(num_bits)]
print(f"Random bits: {random_bits}")

# Plot the random bits
plt.bar(range(num_bits), random_bits, tick_label=range(num_bits))
plt.xlabel('Bit index')
plt.ylabel('Random bit value')
plt.title('Quantum Random Number Generation')
plt.show()


