def test_color666_mps_decoder_decode_small_codes_exact_approx(): code = Color666Code(5) exact_decoder = Color666MPSDecoder() approx_decoder = Color666MPSDecoder(chi=16) identity = code.new_pauli() # probabilities prob_dist = BiasedDepolarizingErrorModel(bias=10).probability_distribution(probability=0.1) # coset probabilities exact_coset_ps, _ = exact_decoder._coset_probabilities(prob_dist, identity) approx_coset_ps, _ = approx_decoder._coset_probabilities(prob_dist, identity) print('#exact Pr(G)=', exact_coset_ps) print('#approx Pr(G)=', approx_coset_ps) assert all(_is_close(exact_coset_ps, approx_coset_ps, rtol=1e-11, atol=0)), ( 'Coset probabilites do not satisfy exact Pr(G) ~= approx Pr(G)')
def test_color666_mps_decoder_decode_value(): # expected coset_ps expected_coset_ps = ( mp.mpf('1.4290529991554288e-9'), # I mp.mpf('2.1657729564075353e-13'), # X mp.mpf('2.2541268663248664e-13'), # Y mp.mpf('9.3780172173812118e-12'), # Z ) code = Color666Code(7) decoder = Color666MPSDecoder(chi=16) # error error = code.new_pauli() error.site('X', (2, 1)) # error.site('Y', (3, 1), (4, 2), (7, 3), (9, 6)) error.site('Y', (7, 3)) error.site('Z', (6, 0), (3, 3), (7, 6)) # syndrome syndrome = pt.bsp(error.to_bsf(), code.stabilizers.T) # sample sample = decoder.sample_recovery(code, syndrome) print(sample) # probabilities prob_dist = DepolarizingErrorModel().probability_distribution(0.1) # coset probabilities coset_ps, _ = decoder._coset_probabilities(prob_dist, sample) print('# expected Pr(G)=', expected_coset_ps) print('# actual Pr(G)=', coset_ps) assert all(_is_close(expected_coset_ps, coset_ps, rtol=1e-11, atol=0)), ( 'Coset probabilites do not satisfy expected Pr(G) ~= Pr(G)')
def test_color666_mps_decoder_cosets_probability_triplet_optimisation(): code = Color666Code(5) decoder = Color666MPSDecoder() # probabilities prob_dist = BiasedDepolarizingErrorModel(bias=10).probability_distribution(probability=0.1) # coset probabilities for null Pauli coset_i_ps, _ = decoder._coset_probabilities(prob_dist, code.new_pauli()) coset_x_ps, _ = decoder._coset_probabilities(prob_dist, code.new_pauli().logical_x()) coset_y_ps, _ = decoder._coset_probabilities(prob_dist, code.new_pauli().logical_x().logical_z()) coset_z_ps, _ = decoder._coset_probabilities(prob_dist, code.new_pauli().logical_z()) # expect Pr(iIG) ~= Pr(xXG) assert _is_close(coset_i_ps[0], coset_x_ps[1], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iIG) ~= Pr(xXG)') # expect Pr(iXG) ~= Pr(xIG) assert _is_close(coset_i_ps[1], coset_x_ps[0], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iXG) ~= Pr(xIG)') # expect Pr(iIG) ~= Pr(yYG) assert _is_close(coset_i_ps[0], coset_y_ps[2], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iIG) ~= Pr(yYG)') # expect Pr(iYG) ~= Pr(yIG) assert _is_close(coset_i_ps[2], coset_y_ps[0], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iXG) ~= Pr(xIG)') # expect Pr(iIG) ~= Pr(zZG) assert _is_close(coset_i_ps[0], coset_z_ps[3], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iIG) ~= Pr(zZG)') # expect Pr(iZG) ~= Pr(zIG) assert _is_close(coset_i_ps[3], coset_z_ps[0], rtol=0, atol=0), ( 'Coset probabilites do not satisfy Pr(iZG) ~= Pr(zIG)')
def test_color666_mps_decoder_sample_recovery(error_pauli): error = error_pauli.to_bsf() code = error_pauli.code syndrome = pt.bsp(error, code.stabilizers.T) recovery_pauli = Color666MPSDecoder.sample_recovery(code, syndrome) recovery = recovery_pauli.to_bsf() assert np.array_equal(pt.bsp(recovery, code.stabilizers.T), syndrome), ( 'recovery {} does not give the same syndrome as the error {}'.format(recovery, error)) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'recovery ^ error ({} ^ {}) does not commute with stabilizers.'.format(recovery, error))
def test_color666_mps_decoder_cosets_probability_equivalence(sample_pauli_f, sample_pauli_g): decoder = Color666MPSDecoder() # probabilities prob_dist = BiasedDepolarizingErrorModel(bias=10).probability_distribution(probability=0.1) # coset probabilities coset_f_ps, _ = decoder._coset_probabilities(prob_dist, sample_pauli_f) coset_g_ps, _ = decoder._coset_probabilities(prob_dist, sample_pauli_g) print('#Pr(fG)=', coset_f_ps) print('#Pr(gG)=', coset_g_ps) assert all(_is_close(coset_f_ps, coset_g_ps, rtol=1e-14, atol=0)), ( 'Coset probabilites do not satisfy Pr(fG) ~= Pr(gG)')
def test_color666_mps_decoder_decode(error_pauli, chi, caplog): with caplog.at_level(logging.WARN): error = error_pauli.to_bsf() code = error_pauli.code syndrome = pt.bsp(error, code.stabilizers.T) decoder = Color666MPSDecoder(chi=chi) recovery = decoder.decode(code, syndrome) assert np.array_equal(pt.bsp(recovery, code.stabilizers.T), syndrome), ( 'recovery {} does not give the same syndrome as the error {}'.format(recovery, error)) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'recovery ^ error ({} ^ {}) does not commute with stabilizers.'.format(recovery, error)) assert len(caplog.records) == 0, 'Unexpected log messages: {}'.format(caplog.text)
def test_color666_mps_decoder_cosets_probability_inequality_biased_noise(chi, tol, rtol): code = Color666Code(5) decoder = Color666MPSDecoder(chi=chi, tol=tol) # probabilities prob_dist = BiasedDepolarizingErrorModel(bias=100).probability_distribution(0.1) # coset probabilities for null Pauli coset_ps, _ = decoder._coset_probabilities(prob_dist, code.new_pauli()) coset_i_p, coset_x_p, coset_y_p, coset_z_p = coset_ps # expect Pr(IG) > Pr(YG) > Pr(XG) ~= Pr(ZG) print() print('Pr(IG){!r} > Pr(YG){!r} > Pr(XG){!r} ~= Pr(ZG){!r}. rtol={}'.format( coset_i_p, coset_y_p, coset_x_p, coset_z_p, abs(coset_x_p - coset_z_p) / abs(coset_z_p))) print('types: Pr(IG):{}, Pr(YG):{}, Pr(XG):{}, Pr(ZG):{}'.format( type(coset_i_p), type(coset_y_p), type(coset_x_p), type(coset_z_p))) assert coset_i_p > coset_y_p, 'Coset probabilites do not satisfy Pr(IG) > Pr(YG)' assert coset_y_p > coset_x_p, 'Coset probabilites do not satisfy Pr(YG) > Pr(XG)' assert coset_y_p > coset_z_p, 'Coset probabilites do not satisfy Pr(YG) > Pr(ZG)' assert _is_close(coset_x_p, coset_z_p, rtol=rtol, atol=0), 'Coset probabilites do not satisfy Pr(XG) ~= Pr(ZG)'
def test_color666_mps2d_contract(): code = Color666Code(5) sample = code.new_pauli().site('X', (3, 1)).site('Y', (2, 2)).site('Z', (6, 4)) print() print(code.ascii_art(pauli=sample)) prob_dist = DepolarizingErrorModel().probability_distribution(0.1) tnc = Color666MPSDecoder.TNC() tn = tnc.create_tn(prob_dist, sample) result_forwards = tt.mps2d.contract(tn) print('Forward contraction result: ', repr(result_forwards)) assert isinstance(result_forwards, mp.mpf), 'Contracted tensor network is not an mp.mpf' assert 0 <= result_forwards <= 1, 'Contracted tensor network not within bounds' result_backwards = tt.mps2d.contract(tn, start=-1, step=-1) print('Backward contraction result:', repr(result_backwards)) assert isinstance(result_backwards, mp.mpf), 'Contracted tensor network is not an mp.mpf' assert 0 <= result_backwards <= 1, 'Contracted tensor network not within bounds' assert _is_close(result_forwards, result_backwards, rtol=1e-14, atol=0), ( 'Contracting forwards does not give the same result as contracting backwards' )
def test_color666_mps_functions(): prob_dist = DepolarizingErrorModel().probability_distribution(0.1) tnc = Color666MPSDecoder.TNC() bra = [ tnc.create_q_node(prob_dist, ('I', 'I'), 'nw'), tnc.create_s_node('w'), tnc.create_q_node(prob_dist, ('I', None), 'sw'), ] mpo1 = [ tnc.create_s_node('n'), tnc.create_q_node(prob_dist, ('I', 'I'), 's'), None, ] mpo2 = [ tnc.create_q_node(prob_dist, (None, 'I'), 'ne'), tnc.create_s_node('s'), None, ] ket = [ None, tnc.create_q_node(prob_dist, ('I', None), 'e'), None, ] expected = 0.47831585185185249 # exact contraction for expected result = tt.mps.contract_pairwise(bra, mpo1) result = tt.mps.contract_pairwise(result, mpo2) result = tt.mps.inner_product(result, ket) print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Exact contraction not as expected' # exact contraction with contract_ladder result = tt.mps.contract_pairwise(bra, mpo1) result = tt.mps.contract_pairwise(result, mpo2) result = tt.mps.contract_pairwise(result, ket) result = tt.mps.contract_ladder(result)[0, 0, 0, 0] print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Exact contraction not as expected' # exact contraction with lcf result = tt.mps.left_canonical_form(bra) result = tt.mps.contract_pairwise(result, mpo1) result = tt.mps.left_canonical_form(result) result = tt.mps.contract_pairwise(result, mpo2) result = tt.mps.left_canonical_form(result) result = tt.mps.inner_product(result, ket) print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Exact contraction with lcf not as expected' # exact contraction with rcf result = tt.mps.right_canonical_form(bra) result = tt.mps.contract_pairwise(result, mpo1) result = tt.mps.right_canonical_form(result) result = tt.mps.contract_pairwise(result, mpo2) result = tt.mps.right_canonical_form(result) result = tt.mps.inner_product(result, ket) print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Exact contraction with rcf not as expected' # exact contraction with truncate result = tt.mps.contract_pairwise(bra, mpo1) result, norm1 = tt.mps.truncate(result) result = tt.mps.contract_pairwise(result, mpo2) result, norm2 = tt.mps.truncate(result) result = tt.mps.inner_product(result, ket) result *= norm1 * norm2 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Exact contraction with truncate not as expected' # exact contraction with normalise lcf result, norm1 = tt.mps.left_canonical_form(bra, normalise=True) result = tt.mps.contract_pairwise(result, mpo1) result, norm2 = tt.mps.left_canonical_form(result, normalise=True) result = tt.mps.contract_pairwise(result, mpo2) result, norm3 = tt.mps.left_canonical_form(result, normalise=True) result = tt.mps.inner_product(result, ket) result *= norm1 * norm2 * norm3 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close( expected, result, rtol=1e-14, atol=0), 'Exact contraction with normalise lcf not as expected' # exact contraction with normalise rcf result, norm1 = tt.mps.right_canonical_form(bra, normalise=True) result = tt.mps.contract_pairwise(result, mpo1) result, norm2 = tt.mps.right_canonical_form(result, normalise=True) result = tt.mps.contract_pairwise(result, mpo2) result, norm3 = tt.mps.right_canonical_form(result, normalise=True) result = tt.mps.inner_product(result, ket) result *= norm1 * norm2 * norm3 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close( expected, result, rtol=1e-14, atol=0), 'Exact contraction with normalise rcf not as expected' # approximate contraction with truncate (chi) result = tt.mps.contract_pairwise(bra, mpo1) result, norm1 = tt.mps.truncate(result, chi=4) result = tt.mps.contract_pairwise(result, mpo2) result, norm2 = tt.mps.truncate(result, chi=4) result = tt.mps.inner_product(result, ket) result *= norm1 * norm2 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close( expected, result, rtol=1e-14, atol=0), 'Approx. contraction with truncate (chi) not as expected' # approximate contraction with truncate (tol) result = tt.mps.contract_pairwise(bra, mpo1) result, norm1 = tt.mps.truncate(result, tol=1e-8) result = tt.mps.contract_pairwise(result, mpo2) result, norm2 = tt.mps.truncate(result, tol=1e-8) result = tt.mps.inner_product(result, ket) result *= norm1 * norm2 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close( expected, result, rtol=1e-14, atol=0), 'Approx. contraction with truncate (tol) not as expected' # reversed exact contraction for expected result = tt.mps.contract_pairwise(mpo2, ket) result = tt.mps.contract_pairwise(mpo1, result) result = tt.mps.inner_product(bra, result) print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), 'Reversed exact contraction not as expected' # reversed approximate contraction with truncate (chi) result = tt.mps.contract_pairwise(mpo2, ket) result, norm1 = tt.mps.truncate(result, chi=4) result = tt.mps.contract_pairwise(mpo1, result) result, norm2 = tt.mps.truncate(result, chi=4) result = tt.mps.inner_product(bra, result) result *= norm1 * norm2 print('#result=', result, '#rtol=', abs(result - expected) / abs(expected)) assert _is_close(expected, result, rtol=1e-14, atol=0), ( 'Reversed approx. contraction with truncate (chi) not as expected')
class _FixedDecoder(Decoder): def __init__(self, decoding): self.decoding = decoding def decode(self, code, syndrome, **kwargs): return self.decoding @property def label(self): return 'fixed' @pytest.mark.parametrize('code, error_model, decoder', [ # each code with each valid decoder (Color666Code(5), DepolarizingErrorModel(), Color666MPSDecoder(chi=8)), (FiveQubitCode(), DepolarizingErrorModel(), NaiveDecoder()), (PlanarCode(5, 5), DepolarizingErrorModel(), PlanarCMWPMDecoder()), (PlanarCode(5, 5), DepolarizingErrorModel(), PlanarMPSDecoder(chi=6)), (PlanarCode(5, 5), DepolarizingErrorModel(), PlanarMWPMDecoder()), (PlanarCode(5, 5), DepolarizingErrorModel(), PlanarRMPSDecoder(chi=6)), (PlanarCode(4, 5), BitPhaseFlipErrorModel(), PlanarYDecoder()), (RotatedPlanarCode(7, 7), DepolarizingErrorModel(), RotatedPlanarMPSDecoder(chi=8)), (RotatedPlanarCode(7, 7), DepolarizingErrorModel(), RotatedPlanarRMPSDecoder(chi=8)), (RotatedPlanarCode(7, 7), BiasedDepolarizingErrorModel(100), RotatedPlanarSMWPMDecoder()), (RotatedToricCode(6, 6), BiasedDepolarizingErrorModel(100), RotatedToricSMWPMDecoder()), (SteaneCode(), DepolarizingErrorModel(), NaiveDecoder()), (ToricCode(5, 5), DepolarizingErrorModel(), ToricMWPMDecoder()), # each generic noise model (PlanarCode(5, 5), BiasedDepolarizingErrorModel(10), PlanarMPSDecoder(chi=6)), (PlanarCode(5, 5), BiasedYXErrorModel(10), PlanarMPSDecoder(chi=6)),
def test_color666_mps_decoder_new_invalid_parameters(chi, tol): with pytest.raises((ValueError, TypeError), match=r"^Color666MPSDecoder") as exc_info: Color666MPSDecoder(chi=chi, tol=tol) print(exc_info)
def test_color666_mps_decoder_new_valid_parameters(chi, tol): Color666MPSDecoder(chi=chi, tol=tol) # no error raised
def test_color666_mps_decoder_properties(): decoder = Color666MPSDecoder(chi=8, tol=1e-8) assert isinstance(decoder.label, str) assert isinstance(repr(decoder), str) assert isinstance(str(decoder), str)