% Error Detection Circuit
% Author: Michel Barbeau
% Version: March 25, 2016
% Dependency: Quantum Information Toolkit & class helper
CNOT=helper.ACNOT;
I=helper.I;
%
%%% code word preperation
%
ket_psi=state('1');
in=tensor(ket_psi,state('00'))
%
%%% Data qubit to code word mapping
%
% 1st CNOT
temp1=u_propagate(in,tensor(CNOT,I))
% reorder qubits 2 & 3
temp2=reorder(temp1,[1 3 2])
% 2nd CNOT
temp3=u_propagate(temp2,tensor(CNOT,I))
% reorder qubits 2 & 3
codeword=reorder(temp3,[1 3 2])
%
%%% code word with single bit error
%
codeword=state('010')
%
%%% Error detection
%
in=tensor(codeword,state('00'))
%%% 1st CNOT
temp1=reorder(in,[1 4 3 2 5]); % reorder qubits 2 & 4
temp2=u_propagate(temp1,tensor(CNOT,I,I,I));
in=reorder(temp2,[1 4 3 2 5]); % reorder qubits 2 & 4
%%% 2nd CNOT
temp1=reorder(in,[1 2 4 3 5]); % reorder qubits 3 & 4
temp2=u_propagate(temp1,tensor(I,CNOT,I,I));
in=reorder(temp2,[1 2 4 3 5]); % reorder qubits 3 & 4
%%% 3rd CNOT
temp1=reorder(in,[1 2 5 4 3]); % reorder qubits 3 & 5
temp2=u_propagate(temp1,tensor(I,CNOT,I,I));
in=reorder(temp2,[1 2 5 4 3]); % reorder qubits 3 & 5
%%% 4th CNOT
temp1=reorder(in,[1 2 3 5 4]); % reorder qubits 4 & 5
temp2=u_propagate(temp1,tensor(I,I,CNOT,I));
in=reorder(temp2,[1 2 3 5 4]); % reorder qubits 4 & 5
display(in);
% Measure the 4th qubit
[~, b1, in]=measure(in, 4);b1=b1-1;
display(b1);
% Measure the 5th qubit
[~, b2, in]=measure(in, 5);b2=b2-1;
display(b2);




