def test_planar_cmwpm_decoder_null_decoding(): code = PlanarCode(3, 3) error = code.new_pauli().site('Y', (2, 0), (2, 4)).to_bsf() syndrome = pt.bsp(error, code.stabilizers.T) decoder_null = PlanarCMWPMDecoder(max_iterations=0) recovery = decoder_null.decode(code, syndrome) assert np.all(recovery == 0), 'Null decoder does not return null recovery'
def test_planar_cmwpm_decoder_decode(error_pauli): error = error_pauli.to_bsf() code = error_pauli.code decoder = PlanarCMWPMDecoder() syndrome = pt.bsp(error, code.stabilizers.T) 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))
def test_planar_cmwpm_step_grid_tight_box(): """ Matches {((0, 1), (4, 5))}: ──X───┬───┬── │ │ │ ──┼───┼───┼── │ │ │ ──┴───┴───X── """ code = PlanarCode(3, 4) # error = code.new_pauli().site('Z', (0, 2), (1, 3), (2, 4), (3, 5)).to_bsf() # syndrome = pt.bsp(error, code.stabilizers.T) # matches = {tuple(code.syndrome_to_plaquette_indices(syndrome))} # {((0, 1), (4, 5))} # print(code.ascii_art(syndrome=syndrome)) # print(matches) grid = PlanarCMWPMDecoder.StepGrid(code) # set background from matches grid.set_background({((0, 1), (4, 5))}, factor=3, initial=1, box_shape='t') # expected grid. Note: border of virtual indices around grid. expected = np.array([[3., 0., 3., 0., 3., 0., 3., 0., 3.], [0., 3., 0., 1., 0., 1., 0., 3., 0.], [3., 0., 1., 0., 1., 0., 1., 0., 3.], [0., 3., 0., 1., 0., 1., 0., 3., 0.], [3., 0., 1., 0., 1., 0., 1., 0., 3.], [0., 3., 0., 1., 0., 1., 0., 3., 0.], [3., 0., 3., 0., 3., 0., 3., 0., 3.]]) assert np.array_equal(grid._grid, expected), 'Tight box not expected shape.' # test with alternative corners grid.set_background({((0, 5), (4, 1))}, factor=3, initial=1, box_shape='t') assert np.array_equal( grid._grid, expected), 'Tight box for alternative corners not expected shape.'
def test_planar_cmwpm_step_grid_loose_box(): """ Matches {((0, 1), (4, 5))}: ──X───┬───┬── │ │ │ ──┼───┼───┼── │ │ │ ──┴───┴───X── """ code = PlanarCode(3, 4) grid = PlanarCMWPMDecoder.StepGrid(code) # set background from matches grid.set_background({((0, 1), (4, 5))}, factor=3, initial=1, box_shape='l') # expected grid. Note: border of virtual indices around grid. expected = np.array([[3., 0., 1., 0., 1., 0., 1., 0., 3.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [3., 0., 1., 0., 1., 0., 1., 0., 3.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [3., 0., 1., 0., 1., 0., 1., 0., 3.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [3., 0., 1., 0., 1., 0., 1., 0., 3.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.' # test with alternative corners grid.set_background({((0, 5), (4, 1))}, factor=3, initial=1, box_shape='l') assert np.array_equal( grid._grid, expected), 'Loose box for alternative corners not expected shape.'
def test_planar_mps_decoder_new_valid_parameters(factor, max_iterations, box_shape, distance_algorithm): PlanarCMWPMDecoder( factor=factor, max_iterations=max_iterations, box_shape=box_shape, distance_algorithm=distance_algorithm) # no error raised
def test_planar_mps_decoder_new_invalid_parameters(factor, max_iterations, box_shape, distance_algorithm): with pytest.raises((ValueError, TypeError), match=r"^PlanarCMWPMDecoder") as exc_info: PlanarCMWPMDecoder(factor=factor, max_iterations=max_iterations, box_shape=box_shape, distance_algorithm=distance_algorithm) print(exc_info)
def test_planar_cmwpm_decoder_overflow(caplog): """ Error: ·─┬─·─┬─·─┬─·─┬─· · · · · ·─┼─Y─┼─·─┼─·─┼─· · Y · · ·─┼─·─┼─·─┼─·─┼─· · · · · ·─┼─·─┼─·─┼─Z─┼─· · · · · ·─┴─·─┴─·─┴─·─┴─· Syndrome: ──┬───┬───┬───┬── │ Z │ │ │ ──X───┼───┼───┼── │ │ Z │ │ ──┼───X───┼───┼── │ │ │ │ ──┼───┼───X───X── │ │ │ │ ──┴───┴───┴───┴── """ factor = 1e+308 # This is just a bit smaller than max float, so it causes overflow with multiple matched indices code = PlanarCode(5, 5) error = code.new_pauli().site('Y', (2, 2), (3, 3)).site('Z', (6, 6)).to_bsf() syndrome = pt.bsp(error, code.stabilizers.T) decoder_cmwpm = PlanarCMWPMDecoder(factor=factor, max_iterations=4, box_shape='t', distance_algorithm=4) # show cmwpm succeeds print() recovery = decoder_cmwpm.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'CMWPM recovery does not commute with stabilizers.') assert np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'CMWPM recovery does not commute with logicals.') assert 'FPE RAISED FloatingPointError' in caplog.text, 'FloatingPointError not logged' print(caplog.text)
def test_planar_cmwpm_decoder_even_diagonal_correlated_error(): """ ·─┬─·─┬─·─┬─·─┬─· · · · · ·─┼─·─┼─·─┼─·─┼─· · Y · · ·─┼─·─┼─Y─┼─·─┼─· · · Y · ·─┼─·─┼─·─┼─Y─┼─· · · · · ·─┼─·─┼─·─┼─·─┼─· · · · · ·─┼─·─┼─·─┼─·─┼─· · · · · ·─┴─·─┴─·─┴─·─┴─· """ code = PlanarCode(7, 5) error = code.new_pauli().site('Y', (3, 3), (4, 4), (5, 5), (6, 6)).to_bsf() syndrome = pt.bsp(error, code.stabilizers.T) decoder_mwpm = PlanarCMWPMDecoder(max_iterations=1) decoder_cmwpm_t_1 = PlanarCMWPMDecoder(factor=3, max_iterations=4, box_shape='t', distance_algorithm=1) decoder_cmwpm_t_2 = PlanarCMWPMDecoder(factor=3, max_iterations=4, box_shape='t', distance_algorithm=2) # show mwpm fails recovery = decoder_mwpm.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'MWPM recovery does not commute with stabilizers.') assert not np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'MWPM recovery does commute with logicals.') # show cmwpm_t_1 fails recovery = decoder_cmwpm_t_1.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'CMWPM (t,1) recovery does not commute with stabilizers.') assert not np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'CMWPM (t,1) recovery does commute with logicals.') # show cmwpm_t_2 succeeds recovery = decoder_cmwpm_t_2.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'CMWPM (t,2) recovery does not commute with stabilizers.') assert np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'CMWPM (t,2) recovery does not commute with logicals.')
def test_planar_cmwpm_decoder_simple_correlated_error(): """ ·─┬─·─┬─· · · Y─┼─·─┼─Y · · ·─┴─·─┴─· """ code = PlanarCode(3, 3) error = code.new_pauli().site('Y', (2, 0), (2, 4)).to_bsf() syndrome = pt.bsp(error, code.stabilizers.T) decoder_mwpm = PlanarCMWPMDecoder(max_iterations=1) decoder_cmwpm = PlanarCMWPMDecoder(factor=3, max_iterations=4, box_shape='t', distance_algorithm=1) # show mwpm fails recovery = decoder_mwpm.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'MWPM recovery does not commute with stabilizers.') assert not np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'MWPM recovery does commute with logicals.') # show cmwpm succeeds recovery = decoder_cmwpm.decode(code, syndrome) assert np.all(pt.bsp(recovery ^ error, code.stabilizers.T) == 0), ( 'CMWPM recovery does not commute with stabilizers.') assert np.all(pt.bsp(recovery ^ error, code.logicals.T) == 0), ( 'CMWPM recovery does not commute with logicals.')
def test_planar_cmwpm_step_grid_fitted_vertical_line(): """ Matches {((0, 3), (4, 3))}: ──┬───X───┬── │ │ │ ──┼───┼───┼── │ │ │ ──┴───X───┴── """ code = PlanarCode(3, 4) grid = PlanarCMWPMDecoder.StepGrid(code) # set background from matches grid.set_background({((0, 3), (4, 3))}, factor=3, initial=1, box_shape='f') # expected grid. Note: border of virtual indices around grid. expected = np.array([[3., 0., 3., 0., 3., 0., 3., 0., 3.], [0., 3., 0., 3., 0., 3., 0., 3., 0.], [3., 0., 3., 0., 1., 0., 3., 0., 3.], [0., 3., 0., 1., 0., 1., 0., 3., 0.], [3., 0., 3., 0., 1., 0., 3., 0., 3.], [0., 3., 0., 3., 0., 3., 0., 3., 0.], [3., 0., 3., 0., 3., 0., 3., 0., 3.]]) assert np.array_equal(grid._grid, expected), 'Fitted box not expected shape.'
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)), (PlanarCode(5, 5), BitFlipErrorModel(), PlanarMPSDecoder(chi=6)), (PlanarCode(5, 5), BitPhaseFlipErrorModel(), PlanarMPSDecoder(chi=6)),
def test_planar_cmwpm_decoder_properties(): decoder = PlanarCMWPMDecoder() assert isinstance(decoder.label, str) assert isinstance(repr(decoder), str) assert isinstance(str(decoder), str)
def test_planar_cmwpm_step_distance(): """ Grid: [[3 0 3 0 3 0 3 0 3] [0 3 0 1 0 1 0 3 0] [3 0 1 0 1 0 1 0 3] [0 3 0 1 0 1 0 3 0] [3 0 1 0 1 0 1 0 3] [0 3 0 1 0 1 0 3 0] [3 0 3 0 3 0 3 0 3] [0 3 0 3 0 3 0 3 0] [3 0 3 0 3 0 3 0 3]] """ f = 3 # factor i = 1 # initial code = PlanarCode(4, 4) # prepare grid distance 1: v+h (i.e. vertical + horizontal) grid = PlanarCMWPMDecoder.StepGrid(code) grid.set_background({((0, 1), (4, 5))}, factor=f, initial=i, box_shape='t') assert grid.distance( (1, 0), (5, 4), algorithm=1 ) == 2 * f + 2 * f, 'Distance 1 left-to-bottom not as expected' assert grid.distance( (5, 4), (1, 0), algorithm=1 ) == 2 * i + 2 * i, 'Distance 1 bottom-to-left not as expected' assert grid.distance( (-1, 2), (3, 6), algorithm=1 ) == 2 * i + 2 * i, 'Distance 1 top-to-right not as expected' assert grid.distance( (3, 6), (-1, 2), algorithm=1 ) == 2 * f + 2 * f, 'Distance 1 right-to-top not as expected' assert grid.distance( (1, 0), (3, 6), algorithm=1 ) == 1 * f + 3 * i, 'Distance 1 left-to-right not as expected' assert grid.distance( (3, 6), (1, 0), algorithm=1 ) == 1 * f + 3 * i, 'Distance 1 right-to-left not as expected' assert grid.distance( (-1, 2), (5, 4), algorithm=1 ) == 3 * i + 1 * f, 'Distance 1 top-to-bottom not as expected' assert grid.distance( (5, 4), (-1, 2), algorithm=1 ) == 1 * f + 3 * i, 'Distance 1 bottom-to-top not as expected' # prepare grid distance 2: min(v+h, h+v) grid = PlanarCMWPMDecoder.StepGrid(code) grid.set_background({((0, 1), (4, 5))}, factor=3, initial=1, box_shape='t') assert grid.distance( (1, 0), (5, 4), algorithm=2 ) == 2 * i + 2 * i, 'Distance 2 left-to-bottom not as expected' assert grid.distance( (5, 4), (1, 0), algorithm=2 ) == 2 * i + 2 * i, 'Distance 2 bottom-to-left not as expected' assert grid.distance( (-1, 2), (3, 6), algorithm=2 ) == 2 * i + 2 * i, 'Distance 2 top-to-right not as expected' assert grid.distance( (3, 6), (-1, 2), algorithm=2 ) == 2 * i + 2 * i, 'Distance 2 right-to-top not as expected' assert grid.distance( (1, 0), (3, 6), algorithm=2 ) == 3 * i + 1 * f, 'Distance 2 left-to-right not as expected' assert grid.distance( (3, 6), (1, 0), algorithm=2 ) == 3 * i + 1 * f, 'Distance 2 right-to-left not as expected' assert grid.distance( (-1, 2), (5, 4), algorithm=2 ) == 3 * i + 1 * f, 'Distance 2 top-to-bottom not as expected' assert grid.distance( (5, 4), (-1, 2), algorithm=2 ) == 3 * i + 1 * f, 'Distance 2 bottom-to-top not as expected' # prepare grid distance 4: min(v+h, h+v, v+h+v, h+v+h) grid = PlanarCMWPMDecoder.StepGrid(code) grid.set_background({((0, 1), (4, 5))}, factor=3, initial=1, box_shape='t') assert grid.distance( (1, 0), (5, 4), algorithm=4) == 4 * 1, 'Distance 4 left-to-bottom not as expected' assert grid.distance( (5, 4), (1, 0), algorithm=4) == 4 * i, 'Distance 4 bottom-to-left not as expected' assert grid.distance( (-1, 2), (3, 6), algorithm=4) == 4 * i, 'Distance 4 top-to-right not as expected' assert grid.distance( (3, 6), (-1, 2), algorithm=4) == 4 * i, 'Distance 4 right-to-top not as expected' assert grid.distance( (1, 0), (3, 6), algorithm=4) == 4 * i, 'Distance 4 left-to-right not as expected' assert grid.distance( (3, 6), (1, 0), algorithm=4) == 4 * i, 'Distance 4 right-to-left not as expected' assert grid.distance( (-1, 2), (5, 4), algorithm=4) == 4 * i, 'Distance 4 top-to-bottom not as expected' assert grid.distance( (5, 4), (-1, 2), algorithm=4) == 4 * i, 'Distance 4 bottom-to-top not as expected'
def test_planar_cmwpm_step_grid_loose_box_off_boundaries(): """ Matches (various off boundary): ──┬───┬───┬── │ │ │ ──┼───┼───┼── │ │ │ ──┴───┴───┴── """ code = PlanarCode(3, 4) grid = PlanarCMWPMDecoder.StepGrid(code) # set background from matches grid.set_background({((0, -1), (2, 3))}, factor=3, initial=1, box_shape='l') # top-off-left to bulk # expected grid. Note: border of virtual indices around grid. expected = np.array([[1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 1., 0., 1., 0., 1., 0., 3., 0.], [1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 1., 0., 1., 0., 1., 0., 3., 0.], [1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 3., 0., 3., 0., 3., 0., 3., 0.], [3., 0., 3., 0., 3., 0., 3., 0., 3.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.' # set background from matches grid.set_background({((2, 3), (4, 7))}, factor=3, initial=1, box_shape='l') # bulk to bottom-off-right # expected grid. Note: border of virtual indices around grid. expected = np.array([[3., 0., 3., 0., 3., 0., 3., 0., 3.], [0., 3., 0., 3., 0., 3., 0., 3., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.], [0., 3., 0., 1., 0., 1., 0., 1., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.], [0., 3., 0., 1., 0., 1., 0., 1., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.' # set background from matches grid.set_background({((-1, 0), (3, 2))}, factor=3, initial=1, box_shape='l') # left-off-top to bulk # expected grid. Note: border of virtual indices around grid. expected = np.array([[1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 1., 0., 1., 0., 3., 0., 3., 0.], [1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 1., 0., 1., 0., 3., 0., 3., 0.], [1., 0., 1., 0., 1., 0., 3., 0., 3.], [0., 1., 0., 1., 0., 3., 0., 3., 0.], [3., 0., 3., 0., 3., 0., 3., 0., 3.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.' # set background from matches grid.set_background({((1, 4), (5, 6))}, factor=3, initial=1, box_shape='l') # bulk to right-off-bottom # expected grid. Note: border of virtual indices around grid. expected = np.array([[3., 0., 3., 0., 3., 0., 3., 0., 3.], [0., 3., 0., 3., 0., 1., 0., 1., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.], [0., 3., 0., 3., 0., 1., 0., 1., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.], [0., 3., 0., 3., 0., 1., 0., 1., 0.], [3., 0., 3., 0., 1., 0., 1., 0., 1.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.' # set background from matches grid.set_background({((0, -1), (4, 7))}, factor=3, initial=1, box_shape='l') # top-off-left to bottom-off-right # expected grid. Note: border of virtual indices around grid. expected = np.array([[1., 0., 1., 0., 1., 0., 1., 0., 1.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [1., 0., 1., 0., 1., 0., 1., 0., 1.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [1., 0., 1., 0., 1., 0., 1., 0., 1.], [0., 1., 0., 1., 0., 1., 0., 1., 0.], [1., 0., 1., 0., 1., 0., 1., 0., 1.]]) assert np.array_equal(grid._grid, expected), 'Loose box not expected shape.'