هذه الصفحة لم تُترجم بعد. يتم عرض المحتوى باللغة الإنجليزية.
Quantum key distribution
For this Qiskit in Classrooms module, students must have a working Python environment with the following packages installed:
qiskitv2.1.0 or newerqiskit-ibm-runtimev0.40.1 or newerqiskit-aerv0.17.0 or newerqiskit.visualizationnumpypylatexenc
To set up and install the packages above, see the Install Qiskit guide. In order to run jobs on real quantum computers, students will need to set up an account with IBM Quantum® by following the steps in the Set up your IBM Cloud account guide.
This module was tested and used 5 seconds of QPU time. This is an estimate only. Your actual usage may vary.
# Uncomment and modify this line as needed to install dependencies
#!pip install 'qiskit>=2.1.0' 'qiskit-ibm-runtime>=0.40.1' 'qiskit-aer>=0.17.0' 'numpy' 'pylatexenc'
Watch the module walkthrough by Dr. Katie McCormick below, or click here to watch it on YouTube.
Introduction and motivation
There are infinitely many ways of encrypting and decrypting information, and literally thousands of ways have been well-studied. Here, we will restrict ourselves to a very early and very simple method of encryption, called "simple replacement", in order to focus on the quantum part of this protocol. The quantum part could be adapted to many other protocols with relatively few changes.
Simple replacement
A simple replacement encryption is one in which one letter or number is replaced with another, such that there is a 1:1 mapping from the letters and numbers in a message, to the letters and numbers being used in an encrypted sequence. A pop-culture instance of these is the cryptoquote or cryptogram puzzle, in which a quote or phrase is encrypted using simple replacement, and the player is tasked with decrypting it. These are easy to solve if they are long enough. Consider the example:
R WVXRWVW GSZG R’W YVGGVI NZPV GSRH KIVGGB OLMT. GSZG DZB, KVLKOV DROO SZEV ZM VZHRVI GRNV HLOERMT RG. R SLKV R NZWV RG HRNKOV VMLFTS.
People who solve these by hand mostly use tricks involving familiarity with the structure of the language of the original message. For example, in English, the only one-letter words like the encrypted "R" are "a" and "I". The double letters encrypted in, for example, "KIVGGB" can only take certain values. There are subtler things that give clues like the most common word fitting the "GSZG" pattern is "that". People using code to solve this have many more options, including simply scanning through possibilities until an English word is recovered, and updating while preserving that word. One simple but powerful method is using letter frequency, especially when the message is long enough to constitute a representative sample of English.
Check-in question
Try your hand at decrypting this if you like, though it is not necessary for the rest of the module. Click the carrat below to see the message.
Answer:
I decided that I’d better make this pretty long. That way, people will have an easier time solving it. I hope I made it simple enough.
The example above is associated with a "key", a mapping from the encrypted to the decrypted letters. In this case, the key is:
- A (not used, let’s call it Z)
- B->Y
- C (not used, let’s call it X)
- D->W
- E->V
- F->U
- ...
And so on. To put it mildly, this is not a good key. Keys in which the encrypted and decrypted letters are simply shifted version of the alphabet (like A->B and B->C) are called "Caesar shift" ciphers.
Note that these are very difficult if they are short. In fact, if they are very short, they are indeterminate. Consider:
URYYP
There are many possible decryptions, using different keys: HELLO, PETTY, HAPPY, JIGGY, STOOL. Can you think of others?
But if you send many messages like this, eventually, the encryption will be cracked. So, you shouldn’t use the same “key” too often. In fact, best is if you use a certain substitution only once. Not in only one message, but only for one single character! By this, we mean you’ll have an encryption scheme or key for each character used in the message, in order. If you want to send a message to a friend using this message, you and your friend would need a pad a paper (in ye olden times) on which this ever-changing key is written. You will use this only once. This is called a “one-time pad”.
The one-time pad
Let’s see how this works with an example. One could do this entirely with letters, but it is common to convert from letters to numbers, say, by assigning A=0, B=1, C=2…. Suppose we are friends involved in clandestine activities and we have shared a pad. Ideally, we would share many pads, but today’s is:
EDGRPOJNCUWQZVMK…
Or, converting to numbers by placement in the alphabet:
4,3,6,17,15, 14, 9, 13, 2, 20, 22, 16, 25, 21, 12, 10…
Let us suppose, I want to share with you, the message:
“I love quantum!”
Or, equivalently:
8, 11, 14, 21, 4, 16, 20, 0, 13, 19, 20, 12
We don’t want to send the above code; that is a simple substitution, which is not at all secure. We want to combine this with our key in some way. A common way is addition modulo 26. We add the value of the message to the value of the key, mod 26, until we reach the end of the message. So, we would send
8+4 (mod 26) = 12, 11+3 (mod 26) = 14, 14+6 (mod 26) = 20, 21+17 (mod 26) = 12…
= 12, 14, 20, 12, 19, 4, 3, 13, 15, 13, 16, 2
Note that if someone intercepts this and does NOT have the key, decrypting it is utterly hopeless! Not even the two “u”s in "quantum" are encoded with the same number! The first is a 3, and the second is a 16… in the same word!
So, I send this to you, and you have the same key I do. You undo the addition modulo 26 which you know I carried out:
12, 14, 20, 12, 19, 4, 3, 13, 15, 13, 16, 2
=(4+x1) (mod 26), (3+x2) (mod 26), (6+x3) (mod 26), (17+x4) (mod 26),…
Such that the message x1, x2, x3, x4… must be
8, 11, 14, 21…
Finally, converting this to text, we have
“I love quantum”.
This is a one-time pad.
Note that if the key is shorter than the message, we start to repeat our encoding. That would still be a hard decryption problem to solve, but not impossible if it is repeated enough times. So, you need a long key (or “pad”).
In many contexts, students will already be familiar with this encryption, such that this activity can be skipped. But it is a relatively quick, simple refresher.
Step 1: Get a partner, and share a sequence of 4 letters to use as a key. Any class-appropriate 4-letter sequence will do.
Step 2: Select a 4-letter secret word you want to send to your partner (both partners do this so you send each other different secret words)
Step 3: Convert the 4-letter key/pad and each of the 4-letter secret words to numbers using A = 1, B = 2, and so on.
Step 4: Combine your 4-letter word with the one-time pad using modulo 26 addition.
Step 5: Hand your partner the sequence of numbers encoding your secret word, and your partner will hand you theirs.
Step 6: Decode each other’s words using modulo 26 subtraction.
Step 7: Verify. Did it work?
Follow-up
Swap encrypted words with a different group, that does not have access to your one-time pad. Can you decrypt it? Explain why or why not?
Hopefully the activity above makes it clear that a one-time pad is an unbreakable form of encryption, given a few assumptions, like:
- The key is the same length as the message being sent, or longer
- The key is truly random
- The key is used only once and then discarded
So this is great. We have unbreakable encryption... unless someone gets our key. If someone gets our key, everything is decrypted. This difference between unbreakable encryption and having all our secrets exposed makes the sharing of a secure key extremely important. The goal of quantum key distribution is to leverage constraints that nature has imposed on quantum information to secure a shared key/one-time pad.
Using quantum states as a key
Let's assume we are working with qubits (emphasizing that qubits have two eigenstates). One could use quantum systems with higher numbers of quantum states, but the state-of-the-art quantum computers at IBM® use qubits. It’s no problem to encode our A, B, C, into sequences of 0’s and 1’s. So, it is sufficient for us to share a key of 0’s and 1’s and do addition modulo 2 on each bit storing a letter.
Check your understanding
Read the question below, think about your answer, then click the triangle to reveal the solution.
If we really only care about English letters, how many bits do we need?
Answer:
Our friends, Alice and Bob would like to share a quantum key in such a way that no one else can intercept it (at least not without them knowing). They need to have a way of sending quantum states to each other. Doing this with high fidelity and no noise/errors is NOT trivial. But there are two approaches tha we should be able to understand at this point:
- A fiber-optic cable allows you to send light… which is very quantum-mechanical. Single photons can be detected with high fidelity over many kilometers of fiber optic cable. This is not a perfect, error-free quantum channel, but it could be very good.
- We could use quantum teleportation, as described in a previous module. That is, Alice and Bob could share entangled qubits and a state could be sent from Alice to Bob using the teleportation protocol.
For this module, we don't want to require you to have high-fidelity optic setups for sharing photons, so we will use the second method for sharing quantum states. But this is not to say that it is the most realistic for long-distance sharing of quantum keys.
We will now explore a protocol first laid out by Charles Bennett and Gilles Brassard in 1984 for sharing states measured in different bases from Alice to Bob. We will use a clever measurement regimen to build up a key for use in later encryption. In other words, we are distributing a quantum key between two parties who wish to communicate, hence "quantum key distribution" (QKD).
QKD step 1: Alice's random bits and random bases
Alice will start out by generating a random sequence of 0's and 1's. She will then randomly select a basis in which to prepare a quantum state, based on each random bit, using the table below (a table that Bob also has):
| Basis | bit = 0 | bit = 1 |
|---|---|---|
| Z | ||
| X |
For example, let us suppose Alice randomly generated a 0, and randomly selected the X basis. Then she would prepare a quantum state . One can certainly leverage quantum randomness to generate a random set of 0's and 1's, and a random basis choice. For now, let's simply assume a random set has been generated, as follows:
| Alice's bits | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | ... |
|---|---|---|---|---|---|---|---|---|---|---|
| Alice's bases | X | X | Z | Z | Z | X | Z | Z | X | ... |
| Alice's states | ... |
This set of random bits, bases, and resulting states would continue in a long sequence, to give a key of sufficient length.
QKD step 2: Bob's random bases
Bob also makes a random choice of bases. However, whereas Alice was using the basis choice to prepare her state, Bob will actually make measurements in these bases. If Bob makes a measurement in the same basis in which Alice prepared the the state, then we can predict the outcome of Bob's measurement. When Bob happens to pick a different basis from the basis Alice used in preparation, we cannot know the outcome of Bob's measurement.
| Alice's bits | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | ... |
|---|---|---|---|---|---|---|---|---|---|---|
| Alice's bases | X | X | Z | Z | Z | X | Z | Z | X | ... |
| Alice's states | ... | |||||||||
| Bob's bases | X | Z | X | Z | X | X | Z | X | X | ... |
| Bob's states (a priori) | ? | ? | ? | ? | ... | |||||
| Bob's states (measured) | ... | |||||||||
| In the table below, consider the first column. Alice has prepared the state which is an eigenstate of X. Since Bob has also randomly chosen to measure in the X basis, there is only one possible outcome for Bob's measured state: In the second column, however, they have chosen different bases. The state Alice sent is This has a 50% chance of being measured by Bob in the state, and a 50% chance of being measured in So the row showing what we know, a priori, about Bob's measurements cannot be filled in for column 2. But Bob will make a measurement and obtain an eigenstate of (in that column) Z. In the bottom row, we fill in what these measurements happened to yield. |
QKD step 3: Public discussion of bases
Alice and Bob can now share with each other what basis they chose in each case. For all the columns in which they happened to choose the same basis, they each know for certain what state the other had. Bob can convert the state and basis to a 0 or 1 according to the convention shared by both parties. We can rewrite the table above to show only the instances where Alice's and Bob's bases matched:
| Alice's bits | 0 | 0 | 1 | 0 | 0 | ... |
|---|---|---|---|---|---|---|
| Alice's bases | X | Z | X | Z | X | ... |
| Alice's states | ... | |||||
| Bob's bases | X | Z | X | Z | X | X |
| Bob's states (a priori) | ... | |||||
| Bob's states (measured) | ... | |||||
| Bob's bits | 0 | 0 | 1 | 0 | 0 | ... |
Alice has successfully transmitted the bit string 00100... to Bob. If the friends agreed ahead of time to use 5-bit strings as numbers in their one-time pad, these first five bits would give them the number
QKD step 4: Verify and send secret
Before Alice and Bob go any further, they should choose a subset of their classical bits to compare. Since they have only kept measurements of qubits which were prepared and measured using the same basis, all the measured values should agree. If there were a very small percentage that did not agree, this could be attributable to quantum noise or errors. But if many do not agree, something has gone wrong!
Here we will not address what fraction of the key should be used for verification. For now, we will assume that this check goes well; we will revisit this in the section below on eavesdropping.
The friends would then send an encrypted message to each other using classical channels. They would then use the numbers in their one-time pad to encrypt/decrypt secret messages, without ever transmitting the one-time pad from one location to another. For the next section on eavesdropping, please keep in mind that all this sharing of the key happens prior to the revelation of the encrypted secret via classical channels.
Alice and Bob communicated their basis of choice via classical channels, so couldn't that be intercepted? Yes! But knowing the basis they used for measurement does not tell you what bit they sent or obtained. That is only possible if you also know Alice's starting bits. But then you would be in Alice's computer, where the secrets are stored, and secret communication of the secrets becomes moot. So interception of the classical communication does not break the encryption. But what about intercepting information in the quantum channel?
Resistance of QKD to eavesdropping
Alice and Bob have a friend Eve, who is notorious for eavesdropping. Eve wishes to intercept Alice's and Bob's quantum key, so that she may use it to decrypt messages sent between the two. This would necessarily happen between Alice's preparation of the states and Bob's measurement of the states, since the measurement collapses the quantum state. In particular, this means the eavesdropping would have to occur before there has been any sharing or comparison of bases.
Eve must guess which base was used in encoding each bit. Again, if she is not able to access Alice's computer, she has nothing on which to base this guess, and it will be random. Let us assume Alice's start is the same as before, and let us further assume that Bob's random choice of measurement basis is the same as before. Let's fill in what Eve obtains if she makes measurements of the quantum channel. As before, if Eve happens to choose the same basis as Alice, we know what she will obtain. If not, she could obtain either of two outcomes, each with a 50% probability.
| Alice's bits | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | ... |
|---|---|---|---|---|---|---|---|---|---|---|
| Alice's bases | X | X | Z | Z | Z | X | Z | Z | X | ... |
| Alice's states | ... | |||||||||
| Eve's guess bases | Z | X | X | Z | X | Z | Z | X | X | ... |
| Eve's states (a priori) | ? | ? | ? | ? | ? | ... | ||||
| Eve's states (measured) | ... | |||||||||
| Bob's bases | X | Z | X | Z | X | X | Z | X | X | ... |
Now because Eve has no idea whether she matched Alice's basis or not, she does not know what to transmit on to Bob to match Alice's original states. When Eve measures, for example, all she knows for certain is that Alice did not prepare the state for that qubit. But Alice could have prepared or All could be consistent with Eve's measurement. So Eve must make a choice. She might send on exactly the state she measured, or she might try to guess instances in which her measurement was not the eigenstate sent by Alice. We will include a mixture in our table:
| Alice's bits | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | ... |
|---|---|---|---|---|---|---|---|---|---|---|
| Alice's bases | X | X | Z | Z | Z | X | Z | Z | X | ... |
| Alice's states | ... | |||||||||
| Eve's guess bases | Z | X | X | Z | X | Z | Z | X | X | ... |
| Eve's states (a priori) | ? | ? | ? | ? | ? | ... | ||||
| Eve's states (measured) |