% Bit-flip error correcting code example for Quack! % by Peter Rohde quack; disp('Bit-flip error correcting code example'); disp(' '); init_state(5); % Qubit labels redundant1 = 1; redundant2 = 2; redundant3 = 3; syndrome1 = 4; syndrome2 = 5; % Prepare input state prepare_plus(redundant1); % What's the input state disp('Input state:'); print_rdm(redundant1); disp(' '); % Encoding cnot(redundant1,redundant2); cnot(redundant1,redundant3); % Apply a bit-flip to one of the qubits channels n = input('Apply bit-flip to which qubit? '); X(n); % Syndrome measurement cnot(redundant1,syndrome1); cnot(redundant2,syndrome1); cnot(redundant2,syndrome2); cnot(redundant3,syndrome2); outcome1 = Z_measure(syndrome1); outcome2 = Z_measure(syndrome2); disp(['Syndrome measured: ' num2str(outcome1) num2str(outcome2)]); disp(' '); % Error correction if outcome1 == 1 && outcome2 == 1 disp('Applied no correction'); end if outcome1 == 1 && outcome2 == -1 X(redundant3); disp('Applited X3 correction'); end if outcome1 == -1 && outcome2 == 1 X(redundant1); disp('Applied X1 correction'); end if outcome1 == -1 && outcome2 == -1 X(redundant2); disp('Applied X2 correction'); end disp(' '); % Decoding cnot(redundant1,redundant3); cnot(redundant1,redundant2); % Output state disp('Reduced density matrix of decoded state:'); print_rdm(redundant1); % Show the circuit history print_hist;