Exemple #1
0
def test_planar_y_decoder_decode_equal_coset_probabilities():
    code = PlanarCode(2, 3)
    decoder = PlanarYDecoder()
    # The following error Pauli gives identical coset probabilities:
    # I-+-I-+-Y
    #   I   I
    # Y-+-Y-+-I
    # So we expect approximately equal success and failure
    error_pauli = PlanarCode(2, 3).new_pauli().site('Y', (2, 0), (2, 2),
                                                    (0, 4))
    # count success and fail
    success, fail = 0, 0
    # run simulations
    error = error_pauli.to_bsf()
    syndrome = pt.bsp(error, code.stabilizers.T)
    for _ in range(2000):
        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))
        if np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0):
            success += 1
        else:
            fail += 1
    assert _is_close(
        success, fail, rtol=0.2, atol=0
    ), 'Success and fail not equally likely with equal coset probabilities'
Exemple #2
0
def test_planar_y_decoder_coset_probability(prob_dist, pauli,
                                            expected_probability_text):
    with mp.workdps(50):
        expected_probability = mp.mpf(expected_probability_text)
        # calculate coset probabilities
        y_stabilizers = PlanarYDecoder._y_stabilizers(pauli.code)
        coset = y_stabilizers ^ pauli.to_bsf(
        )  # numpy broadcasting applies recovery to all stabilizers
        coset_probability = PlanarYDecoder._coset_probability(prob_dist, coset)
        print(repr(coset_probability), repr(expected_probability))
        assert _is_close(expected_probability,
                         coset_probability,
                         rtol=1e-50,
                         atol=0), ('Coset probability not as expected')
Exemple #3
0
def test_planar_y_decoder_y_logical(code):
    y_logical = PlanarYDecoder._y_logical(code)
    assert not np.any(pt.bsp(y_logical, code.stabilizers.T)
                      ), 'Y-logical does not commute with code stabilizers'
    assert np.any(
        pt.bsp(y_logical,
               code.logicals.T)), 'Y-logical does commute with code logicals'
Exemple #4
0
def test_planar_y_decoder_sample_recovery_sans_residual(error_pauli):
    def _mock_residual_recovery(cls, code, syndrome):
        raise AssertionError('Residual recovery called unexpectedly')

    real_residual_recovery = PlanarYDecoder._residual_recovery
    try:
        # mock function
        PlanarYDecoder._residual_recovery = classmethod(
            _mock_residual_recovery)

        error = error_pauli.to_bsf()
        code = error_pauli.code
        syndrome = pt.bsp(error, code.stabilizers.T)
        recovery = PlanarYDecoder._sample_recovery(code, syndrome)
        print()
        print(code.ascii_art(syndrome, code.new_pauli(recovery)))
        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))
    finally:
        # restore real function
        PlanarYDecoder._residual_recovery = real_residual_recovery
Exemple #5
0
def test_planar_y_decoder_partial_recovery(code, syndrome_index,
                                           expected_syndrome_indices):
    partial_recovery = PlanarYDecoder._partial_recovery(code, syndrome_index)
    print()
    print(code.new_pauli(partial_recovery))
    syndrome = pt.bsp(partial_recovery, code.stabilizers.T)
    syndrome_indices = code.syndrome_to_plaquette_indices(syndrome)
    assert syndrome_indices == expected_syndrome_indices, 'syndrome_indices not as expected'
Exemple #6
0
def test_planar_y_decoder_decode(error_pauli):
    error = error_pauli.to_bsf()
    code = error_pauli.code
    decoder = PlanarYDecoder()
    syndrome = pt.bsp(error, code.stabilizers.T)
    recovery = decoder.decode(code, syndrome)
    print()
    print(code.new_pauli(recovery))
    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 np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), (
        'recovery ^ error ({} ^ {}) does not commute with logicals.'.format(
            recovery, error))
Exemple #7
0
def test_planar_y_decoder_partial_recovery_idempotence():
    # tests for bug where destabilizer modified cached return value of _snake_fill
    code = PlanarCode(4, 3)
    syndrome_index = (2, 1)
    # create partial_recovery1, destabilzer and partial_recovery2, copying to test for changes
    partial_recovery1 = np.copy(
        PlanarYDecoder._partial_recovery(code, syndrome_index))
    destabilizer = np.copy(PlanarYDecoder._destabilizer(code, syndrome_index))
    partial_recovery2 = np.copy(
        PlanarYDecoder._partial_recovery(code, syndrome_index))
    print(code.new_pauli(partial_recovery1))
    print(code.new_pauli(destabilizer))
    print(code.new_pauli(partial_recovery2))
    assert np.array_equal(
        partial_recovery1,
        partial_recovery2), '_partial_recovery is not idempotent'
    assert not np.array_equal(
        partial_recovery2, destabilizer), '_partial_recovery == _destabilizer'
Exemple #8
0
def test_planar_y_decoder_destabilizer(code, syndrome_index,
                                       expected_syndrome_indices):
    destabilizer = PlanarYDecoder._destabilizer(code, syndrome_index)
    print()
    print(code.new_pauli(destabilizer))
    syndrome = pt.bsp(destabilizer, code.stabilizers.T)
    syndrome_indices = code.syndrome_to_plaquette_indices(syndrome)
    print(syndrome_indices)
    assert syndrome_indices == expected_syndrome_indices, 'syndrome_indices not as expected'
Exemple #9
0
def test_planar_y_decoder_coset_probability_performance():
    print()
    with mp.workdps(50):
        n_run = 20
        code = PlanarCode(16, 16)  # 16, 16
        prob_dist = (0.9, 0.0, 0.1, 0.0)
        # preload stabilizer cache
        PlanarYDecoder._y_stabilizers(code)
        # time runs
        start_time = time.time()
        for _ in range(n_run):
            coset = PlanarYDecoder._y_stabilizers(code)
            coset_probability = PlanarYDecoder._coset_probability(
                prob_dist, coset)
        print(repr(coset_probability))
        run_time = time.time() - start_time
        print('run_time = {}'.format(run_time))
        # test to avoid regression
        assert run_time < 7  # 5.423123121261597
Exemple #10
0
def test_planar_y_decoder_y_stabilizers(code):
    y_stabilizers = PlanarYDecoder._y_stabilizers(code)
    code_gcd = math.gcd(*code.size)
    assert len(y_stabilizers) == 2**(
        code_gcd - 1), 'There are not 2^(gcd(p,q)-1) y-stabilizers'
    assert not np.any(pt.bsp(y_stabilizers, code.stabilizers.T)
                      ), 'Y-stabilizers do not commute with code stabilizers'
    assert not np.any(pt.bsp(
        y_stabilizers,
        code.logicals.T)), 'Y-stabilizers do not commute with code logicals'
Exemple #11
0
def test_planar_y_decoder_sample_recovery_with_residual(error_pauli):
    error = error_pauli.to_bsf()
    code = error_pauli.code
    syndrome = pt.bsp(error, code.stabilizers.T)
    recovery = PlanarYDecoder._sample_recovery(code, syndrome)
    print()
    print(error_pauli)
    print(code.ascii_art(syndrome, code.new_pauli(recovery)))
    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))
Exemple #12
0
        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)),
    (PlanarCode(5, 5), BitFlipErrorModel(), PlanarMPSDecoder(chi=6)),
    (PlanarCode(5, 5), BitPhaseFlipErrorModel(), PlanarMPSDecoder(chi=6)),
    (PlanarCode(5, 5), CenterSliceErrorModel((0.2, 0.8, 0), 0.5), PlanarMPSDecoder(chi=6)),
    (PlanarCode(5, 5), DepolarizingErrorModel(), PlanarMPSDecoder(chi=6)),
    (PlanarCode(5, 5), PhaseFlipErrorModel(), PlanarMPSDecoder(chi=6)),
])
Exemple #13
0
def test_planar_y_decoder_properties():
    decoder = PlanarYDecoder()
    assert isinstance(decoder.label, str)
    assert isinstance(repr(decoder), str)
    assert isinstance(str(decoder), str)
Exemple #14
0
def test_planar_y_decoder_residual_recovery(code, syndrome_index):
    partial_recovery = PlanarYDecoder._partial_recovery(code, syndrome_index)
    syndrome = pt.bsp(partial_recovery, code.stabilizers.T)
    residual_recovery = PlanarYDecoder._residual_recovery(code, syndrome)
    assert not np.any(partial_recovery ^ residual_recovery
                      ), 'Residual recovery does not match partial recovery'
Exemple #15
0
def test_planar_y_decoder_destabilizer_invalid_code(code, syndrome_index):
    with pytest.raises(ValueError):
        PlanarYDecoder._destabilizer(code, syndrome_index)