Kvantumos hibajavítás
A valódi kvantumcsatornák és -kapuk zajosak: a kvantumbiten bit-flip (X), fázis-flip (Z) vagy mindkettő (Y) hiba léphet fel. A kvantumos hibajavító kódok (Quantum Error Correcting Code, QECC) egy logikai kvantumbitet több fizikai kvantumbitbe kódolnak úgy, hogy egy hiba kiderüljön és kijavítható legyen. Mérni közben nem szabad a kódolt állapotot, hiszen a mérés megváltoztatná; a no-cloning tétel miatt pedig egyszerűen lemásolni sem tudjuk. Ehelyett segéd-kvantumbiteken (ancilla) a hiba helyére utaló szindrómát állítjuk elő, és ennek alapján javítunk.
A tesztkörnyezet
A kódok tesztelését egy segédfüggvény végzi. A run_with_error egy zajmodellel futtat, amelyben minden identitáskapu (id) után adott valószínűséggel Pauli-hiba lép fel (zajmodellek). A test_quantum_qec először minden csatorna-kvantumbiten egyenként szándékosan elkövet egy hibát, és ellenőrzi, hogy a kódolás, szindrómamérés, javítás és dekódolás után visszakapjuk-e a kiinduló állapotot. Ha igen, a hibavalószínűséget 0-tól 0,5-ig növelve megméri, milyen arányban kapjuk vissza a kódolt 1-et (ez a hűség, fidelity), és kirajzolja.
import matplotlib.pyplot as plt
from qiskit.quantum_info import partial_trace,Statevector
import qiskit
from qiskit_aer.noise import NoiseModel, pauli_error
from qiskit_aer import AerSimulator
from qiskit import transpile
### helper stuff
def pauli_error_x(err, _):
return pauli_error([('I', 1 - err), ('X', err)])
def pauli_error_z(err, _):
return pauli_error([('I', 1 - err), ('Z', err)])
def pauli_error_y(err, _):
return pauli_error([('I', 1 - err), ('Y', err)])
def run_with_error(qc, id_error, shots=100, opt_level=0):
noise_model = NoiseModel()
noise_model.add_all_qubit_quantum_error(id_error, 'id')
sim = AerSimulator(method='matrix_product_state', noise_model=noise_model)
# sim = Aer.get_backend('qasm_simulator')
transpiled_qc = transpile(qc, sim, optimization_level=opt_level)
res = sim.run(transpiled_qc, shots=shots).result()
return res
def test_quantum_qec(qc_enc: qiskit.QuantumCircuit | None = None, qc_syndrome: qiskit.QuantumCircuit | None = None,
qc_correct: qiskit.QuantumCircuit | None = None,
qc_decode: qiskit.QuantumCircuit | None = None, qc_type=0):
# 0: bitlfip
# 1: phaseflip
# 2: shor
pad_bits = 4
err_to_apply = qiskit.circuit.library.IGate
err_model = pauli_error_x
channe_qbits = 3
if qc_type == 0:
err_to_apply = qiskit.circuit.library.XGate
elif qc_type == 1:
err_to_apply = qiskit.circuit.library.ZGate
err_model = pauli_error_z
elif qc_type == 2:
pad_bits = 16
err_to_apply = qiskit.circuit.library.YGate
channe_qbits = 9
err_model = pauli_error_y
sv = None
sv_res = None
failed = False
for i in range(channe_qbits):
sv = qiskit.quantum_info.random_statevector(2, seed=209)
sv = sv.from_label("0" * pad_bits).tensor(sv)
qc = qiskit.QuantumCircuit(qc_enc.num_qubits)
qc.compose(qc_enc, inplace=True, wrap=True)
qc.append(err_to_apply(), qargs=[i])
qc.barrier(label="Errors")
if qc_syndrome:
qc.compose(qc_syndrome, inplace=True, wrap=True)
if qc_correct:
qc.compose(qc_correct, inplace=True, wrap=True)
if qc_decode:
qc.compose(qc_decode, inplace=True, wrap=True)
#qc.draw("mpl")
sv_res = sv.evolve(qc)
sv = partial_trace(sv, [i for i in range(1, pad_bits + 1)])
sv_res = partial_trace(sv_res, [i for i in range(1, pad_bits + 1)])
if sv == sv_res:
continue
failed = True
print(f"Error when applying error to the {i}th channel bit!")
if failed:
return
print("Your QEC is working as intended congratulations!")
qc = qiskit.QuantumCircuit(qc_enc.num_qubits,1)
qc.x(0)
qc.compose(qc_enc, inplace=True, wrap=True)
for i in range(channe_qbits):
qc.id(i)
qc.barrier(label="Errors")
if qc_syndrome:
qc.compose(qc_syndrome, inplace=True, wrap=True)
if qc_correct:
qc.compose(qc_correct, inplace=True, wrap=True)
if qc_decode:
qc.compose(qc_decode, inplace=True, wrap=True)
qc.measure(0,0)
fids = []
errs = []
n= 50
for k in range(n):
max_err= 0.5
use_err = k/n*max_err
results = run_with_error(qc,id_error=err_model(use_err,None))
try:
fidelity = results.get_counts()["1"]/100
except:
fidelity =0
fids.append(fidelity)
errs.append(use_err)
plt.xlabel('Error chance')
plt.ylabel('Fidelity')
plt.plot(errs,fids)
A teszt a dekódolás után csak az első csatorna-kvantumbitet (a logikai kvantumbitet) hasonlítja össze a kiinduló állapottal.
A bit-flip kód
Feladat. Készítsük el a bit-flip hiba ellen védő kód áramkörét: a kódolót, a szindrómamérést, a javítást és a dekódolót, három csatorna- és két segéd-kvantumbittel!
A háromkvantumbites bit-flip kód a logikai állapotot -be kódolja: két CNOT a 0. csatorna-kvantumbit értékét „terjeszti szét” a másik kettőre (ez nem másolás, hanem összefonódás, lásd CNOT mint klasszikus másológép):
channel = qiskit.QuantumRegister(3)
ancillas = qiskit.QuantumRegister(2)
qc_encode = qiskit.QuantumCircuit(channel,ancillas,name="enc")
### Encoding
qc_encode.cx(channel[0],channel[1])
qc_encode.cx(channel[0],channel[2])
###
qc_encode.draw("mpl")
A szindróma a csatorna-kvantumbitek páronkénti paritása: az első segéd-kvantumbit a 0. és az 1., a második az 1. és a 2. csatorna-kvantumbit XOR-ja. A kódolt állapot két tagjában ( és ) mindkét paritás 0, ezért a szindróma mérése nem rontja el a szuperpozíciót, egy bit-flip hiba viszont kiderül belőle:
| Hibás kvantumbit | 1. segéd | 2. segéd |
|---|---|---|
| nincs | 0 | 0 |
| 0. | 1 | 0 |
| 1. | 1 | 1 |
| 2. | 0 | 1 |
channel = qiskit.QuantumRegister(3)
ancillas = qiskit.QuantumRegister(2)
qc_shor_bit = qiskit.QuantumCircuit(channel, ancillas, name="syn")
### Syndrome
qc_shor_bit.cx(channel[0], ancillas[0])
qc_shor_bit.cx(channel[1], ancillas[0])
qc_shor_bit.cx(channel[1], ancillas[1])
qc_shor_bit.cx(channel[2], ancillas[1])
###
qc_shor_bit.draw("mpl")
A javítás a szindróma alapján Toffoli-kapukkal billenti vissza a hibás kvantumbitet: a 11 szindrómánál az 1., az 10-nál (első segéd 1, második 0) a 0., a 01-nél a 2. csatorna-kvantumbitet. A hiányzó 0-s vezérlést úgy kapjuk, hogy a Toffoli előtt és után X kaput teszünk a megfelelő segéd-kvantumbitre:
channel = qiskit.QuantumRegister(3)
ancillas = qiskit.QuantumRegister(2)
qc_correct = qiskit.QuantumCircuit(channel,ancillas,name="corr")
### Korrekció
qc_correct.ccx(ancillas[0],ancillas[1],channel[1])
qc_correct.x(ancillas[1])
qc_correct.ccx(ancillas[0],ancillas[1],channel[0])
qc_correct.x(ancillas[1])
qc_correct.x(ancillas[0])
qc_correct.ccx(ancillas[0],ancillas[1],channel[2])
qc_correct.x(ancillas[0])
###
qc_correct.draw("mpl")
A dekódoló a kódoló megfordítása:
channel = qiskit.QuantumRegister(3)
ancillas = qiskit.QuantumRegister(2)
qc_decode = qiskit.QuantumCircuit(channel,ancillas,name="dec")
### Decode
qc_decode.cx(channel[0], channel[1])
qc_decode.cx(channel[0], channel[2])
###
qc_decode.draw("mpl")
A teszt:
# QEC test
plt.close()
test_quantum_qec(qc_enc=qc_encode, qc_syndrome=qc_shor_bit, qc_correct=qc_correct, qc_decode=qc_decode, qc_type=0)
Your QEC is working as intended congratulations!
Kis hibavalószínűségnél a kód szinte minden hibát kijavít; két egyszerre fellépő hibát viszont már nem tud, ezért nagyobb hibavalószínűségnél a hűség csökken. A javító áramkör harmadik Toffoli-kapuja a gyakorlati megoldásban tévesen ismét az 1. csatorna-kvantumbitet célozta, így az ottani hibát visszarontotta, a 2.-at pedig nem javította; a teszt ezt nem vette észre, mert csak a dekódolt első kvantumbitet vizsgálja, amelyet ezek a hibák nem érintenek. Ugyanezért a hűség görbéje is ugyanaz a két változatnál.
A fázis-flip kód
Feladat. Készítsük el a fázis-flip hiba ellen védő kódot!
A fázis-flip hiba (Z) a , bázisban olyan, mint a bit-flip a , bázisban: , . Ezért a bit-flip kódot használjuk, de a kódolás után H kapukkal átváltunk a , bázisba, a szindrómamérés előtt pedig vissza:
ph_enc = qc_encode.copy()
ph_syn = qc_shor_bit.copy()
ph_cor = qc_correct.copy()
ph_dec = qc_decode.copy()
### Your code goes here
h_gates = qiskit.QuantumCircuit(5)
h_gates.h([i for i in range(3)])
ph_enc = ph_enc.compose(h_gates)
ph_syn = h_gates.compose(ph_syn)
###
qc_ph = qiskit.QuantumCircuit(ph_enc.num_qubits)
qc_ph.compose(ph_enc, inplace=True)
qc_ph.barrier(label="encode")
qc_ph.barrier(label="channel")
qc_ph.compose(ph_syn, inplace=True)
qc_ph.barrier(label="syndrome")
qc_ph.compose(ph_cor, inplace=True)
qc_ph.barrier(label="correct")
qc_ph.compose(ph_dec, inplace=True)
qc_ph.barrier(label="decode")
qc_ph.draw("mpl")
### QEC test
plt.close()
test_quantum_qec(qc_enc=ph_enc,qc_syndrome=ph_syn,qc_correct=ph_cor,qc_decode=ph_dec,qc_type=1)
Your QEC is working as intended congratulations!
Shor kódja
Feladat (extra). Készítsük el Shor kilenckvantumbites kódját, amely tetszőleges egykvantumbites hiba (így az Y hiba) ellen is véd!
Megoldás
Shor kódja a két kódot egymásba ágyazza. A logikai kvantumbitet előbb a fázis-flip kóddal három kvantumbitbe (a 0., 3. és 6.), majd ezek mindegyikét a bit-flip kóddal három-három kvantumbitbe kódoljuk. A javítás két lépés: a három blokkban külön-külön bit-flip szindróma és javítás (6 segéd-kvantumbittel), majd a blokkok között fázis-flip szindróma és javítás (további 2 segéd-kvantumbittel).
channel = qiskit.QuantumRegister(9)
ancillas = qiskit.QuantumRegister(8)
qc_encode = qiskit.QuantumCircuit(channel, ancillas, name="enc")
### Your code here
qc_encode.cx(channel[0], channel[3])
qc_encode.cx(channel[0], channel[6])
qc_encode.h(channel[0::3])
for i in range(0, 9, 3):
qc_encode.cx(channel[i], channel[i + 1])
qc_encode.cx(channel[i], channel[i + 2])
###
qc_shor_bit = qiskit.QuantumCircuit(channel, ancillas, name="bitflip")
### Your code here
qc_shor_bit.barrier()
for k in range(0, 3):
ancilla_index = k * 2
i = k * 3
qc_shor_bit.cx(channel[i], ancillas[ancilla_index])
qc_shor_bit.cx(channel[i + 1], ancillas[ancilla_index])
qc_shor_bit.cx(channel[i + 1], ancillas[ancilla_index + 1])
qc_shor_bit.cx(channel[i + 2], ancillas[ancilla_index + 1])
# Correct
qc_shor_bit.ccx(ancillas[ancilla_index], ancillas[ancilla_index + 1], channel[i + 1])
qc_shor_bit.x(ancillas[ancilla_index + 1])
qc_shor_bit.ccx(ancillas[ancilla_index], ancillas[ancilla_index + 1], channel[i])
qc_shor_bit.x(ancillas[ancilla_index + 1])
qc_shor_bit.x(ancillas[ancilla_index])
qc_shor_bit.ccx(ancillas[ancilla_index], ancillas[ancilla_index + 1], channel[i + 2])
qc_shor_bit.x(ancillas[ancilla_index])
qc_shor_bit.barrier()
for i in range(0, 9, 3):
qc_shor_bit.cx(channel[i], channel[i + 1])
qc_shor_bit.cx(channel[i], channel[i + 2])
###
qc_shor_phase = qiskit.QuantumCircuit(channel, ancillas, name="phaseflip")
### Your code here
qc_shor_phase.h(channel[0::3])
qc_shor_phase.cx(channel[0], ancillas[6])
qc_shor_phase.cx(channel[3], ancillas[6])
qc_shor_phase.cx(channel[3], ancillas[7])
qc_shor_phase.cx(channel[6], ancillas[7])
qc_shor_phase.barrier()
qc_shor_phase.ccx(ancillas[6], ancillas[7], channel[3])
qc_shor_phase.x(ancillas[7])
qc_shor_phase.ccx(ancillas[6], ancillas[7], channel[0])
qc_shor_phase.x(ancillas[7])
qc_shor_phase.x(ancillas[6])
qc_shor_phase.ccx(ancillas[6], ancillas[7], channel[6])
qc_shor_phase.x(ancillas[6])
qc_shor_phase.barrier()
qc_shor_phase.cx(channel[0], channel[3])
qc_shor_phase.cx(channel[0], channel[6])
#####
#qc_encode.draw("mpl")
#qc_shor_bit.draw("mpl")
#qc_shor_phase.draw("mpl")
test_quantum_qec(qc_enc=qc_encode, qc_syndrome=qc_shor_bit, qc_correct=qc_shor_phase, qc_type=2)
Your QEC is working as intended congratulations!
Itt a bit-flip blokk javítása a helyes alakú: a harmadik Toffoli a 2. kvantumbitet célozza, előtte és utána X kapuval az első segéd-kvantumbiten.
Forrás: gyak_9.pdf (28–43. oldal)