This experiment uses IBM’s 127-Qubit quantum computer 'Sherbrooke', with Qiskit, to generate a Bitcoin seed phrase consisting of 24 words from the BIP-39 standard list. Quantum computers harness key quantum mechanical properties such as superposition and entanglement, enhancing the system's randomness and security. Initially, each qubit is set in a superposition state, ensuring that its measurement outcome is entirely random. These random outcomes directly select words from the BIP-39 list, effectively increasing the entropy and thereby boosting the cryptographic security of the generated seed phrase.
Code Walkthrough
1. Quantum Circuit Initialization:
The quantum circuit is initialized with 24 qubits. Each qubit corresponds to a potential word in the Bitcoin seed phrase. The initialization process involves setting each qubit into a basic state, denoted as ∣0⟩.
2. Superposition:
Each qubit is then put into a state of superposition using Hadamard gates. The Hadamard gate (H) is applied to each qubit, transforming the initial state ∣0⟩ to (∣0⟩ + ∣1⟩)/sqrt(2). This operation ensures that each qubit has an equal probability of being measured as either 0 or 1.
3. Measurement:
After applying the Hadamard gates, all qubits are measured. The quantum measurement collapses each qubit's superposition state to either 0 or 1 with equal probability. The outcome for the 24 qubits collectively forms a binary string.
4. Conversion to Indices:
The binary string from the measurements is then interpreted as a sequence of numbers. Since the BIP-39 standard consists of 2048 words, each group of qubits must represent a number from 0 to 2047 (which requires at least 11 bits per number since 2^11=2048). Depending on the length of the measurement string and the bit requirements, the binary string is divided accordingly.
5. Word Selection:
Each index obtained from the binary string is used to select a word from the BIP-39 text file. The selected words collectively form the Bitcoin seed phrase.
6. Compilation and Execution on Quantum Backend:
The quantum circuit is compiled for IBM's Sherbrooke backend. The compiled circuit is then executed. To achieve reliable results, 4000 shots are performed during the execution. This high number of shots helps in achieving statistical significance, ensuring that the probabilistic outcomes of quantum measurements are accurately represented. More shots provide a larger dataset, reducing the impact of quantum noise and operational errors on the final measurement outcomes.
7. Output:
The final output is a 24-word Bitcoin seed phrase, generated from true quantum randomness.
Code:
# imports
import json
from qiskit import QuantumCircuit
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, RuntimeOptions
import numpy as np
# Setup IBMQ account
API_KEY = 'Your_IBMQ_Key_Here_O-`' # Replace with IBMQ API key
service = QiskitRuntimeService(channel='ibm_quantum', token=API_KEY)
backend = service.backend('ibm_sherbrooke') # IBM backend
# Load BIP-39 words from text file
with open('/Users/Documents/BIPwords.txt', 'r') as file:
bip39_words = file. read().splitlines()
def generate_seed_phrase(num_qubits, backend):
circ = QuantumCircuit(num_qubits)
for i in range(num_qubits):
circ.h(i) # Apply Hadamard gate to create superposition
circ.measure_all()
# Transpile the circuit for backend
pm = generate_preset_pass_manager(optimization_level=1, target=backend. target)
transpiled_circuits = pm. run([circ])
# Execute the circuit with Qiskit Runtime
options = RuntimeOptions(backend=backend. name)
job = service. run(program_id="sampler", inputs={'circuits': transpiled_circuits}, options=options)
result = job.result()
# Accessing quasi-probabilities from SamplerResult
quasi_probs = result.quasi_dists[0] # Get the quasi-probability distribution of the first (and only) circuit
# Check if quasi_probs is empty or contains zero probabilities
if not quasi_probs or all(prob == 0 for prob in quasi_probs.values()):
raise ValueError("Invalid or zero quasi-probabilities obtained from the quantum computation.")
# Extract probabilities and normalize them
probabilities = list(quasi_probs.values())
normalized_probabilities = np.array(probabilities) / np.sum(probabilities)
# Randomly choose indices based on the normalized probabilities
indices = np.random.choice(len(normalized_probabilities), size=num_qubits, p=normalized_probabilities)
seed_words = [bip39_words[i % 2048] for i in indices]
return seed_words
# Generate the seed phrase using the quantum computer
seed_phrase = generate_seed_phrase(24, backend)
# Print the generated seed phrase
print("Generated Bitcoin Seed Phrase:", ' '.join(seed_phrase))
# end