Link Search Menu Expand Document

Quantum computing with qiskit

In this section, we will explore how to use a quantum computer to generate a private key. Generating a private key involves creating a sequence of random bits, which can be derived from various sources of entropy. Entropy can be as mundane as the path your cat takes during the day or as complex as measuring the state of a qubit in a quantum computer. In both cases, the result is a random number.

For our example, we will use a quantum computer. The principles of quantum mechanics allow us to obtain a random value when we measure the state of a qubit.

Quantum computers use qubits instead of traditional bits. Qubits can exist in a superposition, meaning they can be in both the 0 and 1 states simultaneously with certain probabilities. A qubit is analogous to a coin flip:

  • πŸ’¨ While the coin is in the air, it can be considered to be in both states, heads or tails, with a 50% probability.
  • πŸͺ™ When the coin lands, its state is revealed as either heads or tails.

Although we are far from having quantum computers at home, the qiskit library allows us to simulate one. It also enables to interact with IBM Quantum, where you can use a real quantum computer in the cloud.

You can obtain an API token from IBM Quantum, which will allow you to use the IBM quantum computer for a few minutes per month. You will need this token for the example.

We begin by importing the necessary modules:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import transpile
from qiskit_ibm_runtime import QiskitRuntimeService

Next, we define a circuit for the quantum computer. Remember to replace YOUR_TOKEN with your actual token from IBM Quantum. Here is what the code does:

  • We specify that we want 64 qubits and 64 bits. Ideally, we would need 256 to generate a key with 256 bits, but no computer currently has that many qubits.
  • The h function applies a Hadamard gate, which equalizes the probability of the qubits being in the 0 or 1 state.
  • The measure function indicates that we want to measure the state of the 64 qubits.
  • We perform the process 4 times, as indicated by shots, to generate a key with 256 bits, that is 64*4.
q = QuantumRegister(64)
c = ClassicalRegister(64)
circ = QuantumCircuit(q, c)
circ.h(q)
circ.measure(q, c)

service = QiskitRuntimeService(channel="ibm_quantum", token="YOUR_TOKEN")
backend = service.backend('ibm_kyiv')
qc_basis = transpile(circ, backend)
job = backend.run(qc_basis, shots=4)

counts = job.result().to_dict()["results"][0]["data"]["counts"]
priv_key = ''.join(counts.keys()).replace('0x', '')
print(f"Private key: {priv_key}")

Once completed, you will have generated a private key. This is a random number of 256 bits, with entropy derived from a real quantum computer.

It is important to note that you should not generate private keys in this manner, at least for now. Although the key’s entropy may be good, there is no guarantee that the key is private.

If you have a quantum computer at home, the situation changes, but this is likely not the case.