예제 #1
0
def test_record_time():
    b = qb.CirqBoard(u.squares_to_bitboard(["b8", "b5", "c7"]))
    assert b.perform_moves(
        "b8^a6c6:SPLIT_JUMP:BASIC",
        "b5a6:PAWN_CAPTURE:BASIC",
        "c6b8:JUMP:BASIC",
        "c7c5:PAWN_TWO_STEP:BASIC",
        "b5c6:PAWN_EP:BASIC",
    )
    assert "sample_with_ancilla takes" in b.debug_log
    assert "seconds." in b.debug_log
    assert (float(
        b.debug_log.split("sample_with_ancilla takes ")[-1].split(" seconds.")
        [0]) > 0)

    b.record_time("test_action", 0.12, 0.345)
    assert "test_action takes" in b.debug_log
    expected_time_1 = pytest.approx(0.345 - 0.12, 1e-7)
    assert (float(
        b.debug_log.split("test_action takes ")[-1].split(" seconds.")[0]) ==
            expected_time_1)
    assert len(b.timing_stats) == 2
    assert b.timing_stats["test_action"][-1] == expected_time_1

    b.record_time("test_action", 0.5, 0.987)
    expected_time_2 = pytest.approx(0.987 - 0.5, 1e-7)
    assert (float(
        b.debug_log.split("test_action takes ")[-1].split(" seconds.")[0]) ==
            expected_time_2)
    assert len(b.timing_stats) == 2
    assert len(b.timing_stats["test_action"]) == 2
    assert b.timing_stats["test_action"][-1] == expected_time_2
예제 #2
0
def create_board(processor_name: str, *, noise_mitigation: float):
    return qb.CirqBoard(init_basis_state=0,
                        sampler=utils.get_sampler_by_name(
                            processor_name, gateset='sqrt-iswap'),
                        device=utils.get_device_obj_by_name(processor_name),
                        error_mitigation=enums.ErrorMitigation.Correct,
                        noise_mitigation=noise_mitigation)
예제 #3
0
def test_a11(board):
    b = qb.CirqBoard(
        u.squares_to_bitboard([
            'd1', 'e1', 'h1', 'd2', 'e2', 'f2', 'g2', 'e3', 'e6', 'd7', 'e7',
            'f7'
        ]))
    did_it_move = b.perform_moves(
        'e3^c2f1:SPLIT_JUMP:BASIC',
        'e1g1:KS_CASTLE:BASIC',
    )
    if did_it_move:
        possibilities = [
            u.squares_to_bitboard([
                'd1', 'f1', 'g1', 'd2', 'e2', 'f2', 'g2', 'c2', 'e6', 'd7',
                'e7', 'f7'
            ]),
        ]
    else:
        possibilities = [
            u.squares_to_bitboard([
                'd1', 'e1', 'h1', 'd2', 'e2', 'f2', 'g2', 'f1', 'e6', 'd7',
                'e7', 'f7'
            ]),
        ]

    assert_samples_in(b, possibilities)
예제 #4
0
def test_record_time():
    b = qb.CirqBoard(u.squares_to_bitboard(['b8', 'b5', 'c7']))
    assert b.perform_moves(
        'b8a6c6:SPLIT_JUMP:BASIC',
        'b5a6:PAWN_CAPTURE:BASIC',
        'c6b8:JUMP:BASIC',
        'c7c5:PAWN_TWO_STEP:BASIC',
        'b5c6:PAWN_EP:BASIC',
    )
    assert 'sample_with_ancilla takes' in b.debug_log
    assert 'seconds.' in b.debug_log
    assert float(
        b.debug_log.split('sample_with_ancilla takes ')[-1].split(' seconds.')
        [0]) > 0

    b.record_time('test_action', 0.12, 0.345)
    assert 'test_action takes' in b.debug_log
    expected_time_1 = pytest.approx(0.345 - 0.12, 1e-7)
    assert float(
        b.debug_log.split('test_action takes ')[-1].split(' seconds.')
        [0]) == expected_time_1
    assert len(b.timing_stats) == 2
    assert b.timing_stats['test_action'][-1] == expected_time_1

    b.record_time('test_action', 0.5, 0.987)
    expected_time_2 = pytest.approx(0.987 - 0.5, 1e-7)
    assert float(
        b.debug_log.split('test_action takes ')[-1].split(' seconds.')
        [0]) == expected_time_2
    assert len(b.timing_stats) == 2
    assert len(b.timing_stats['test_action']) == 2
    assert b.timing_stats['test_action'][-1] == expected_time_2
예제 #5
0
def syc54_noiseless(state):
    np.random.seed(get_seed())
    return qb.CirqBoard(
        state,
        device=utils.get_device_obj_by_name("Syc54-noiseless"),
        error_mitigation=enums.ErrorMitigation.Error,
    )
예제 #6
0
def syc23_noisy(state):
    return qb.CirqBoard(
        state,
        sampler=cirq.DensityMatrixSimulator(noise=cirq.ConstantQubitNoiseModel(
            qubit_noise_gate=cirq.DepolarizingChannel(0.005)),
                                            seed=get_seed()),
        device=utils.get_device_obj_by_name('Syc23-simulator'),
        error_mitigation=enums.ErrorMitigation.Correct,
        noise_mitigation=0.10)
예제 #7
0
def test_a8(board):
    b = qb.CirqBoard(u.squares_to_bitboard(['a1', 'd1', 'c5', 'a7', 'h8']))
    did_it_move = b.perform_moves('c5^a4a6:SPLIT_JUMP:BASIC',
                                  'a1a6:SLIDE:CAPTURE')
    if did_it_move:
        possibilities = [u.squares_to_bitboard(['a6', 'd1', 'a7', 'h8'])]
    else:
        possibilities = [u.squares_to_bitboard(['a1', 'd1', 'a4', 'a7', 'h8'])]
    assert_samples_in(b, possibilities)
예제 #8
0
def test_undo_last_move():
    # TODO  (cantwellc) more comprehensive tests
    b = qb.CirqBoard(u.squares_to_bitboard(['a2']))
    assert b.perform_moves('a2a4:PAWN_TWO_STEP:BASIC')
    probs = b.get_probability_distribution(1000)
    assert_prob_about(probs, qb.square_to_bit('a4'), 1.0)
    assert b.undo_last_move()
    probs = b.get_probability_distribution(1000)
    assert_prob_about(probs, qb.square_to_bit('a2'), 1.0)
예제 #9
0
    def __init__(self,
                 size: Optional[int] = 8,
                 reps: Optional[int] = 1000,
                 board=None):

        self.size = size
        self.reps = reps
        self.board = board or qb.CirqBoard(0)

        self._reset_state()
예제 #10
0
def test_path_qubits():
    """Source and target should be in the same line, otherwise ValueError should be returned."""
    b = qb.CirqBoard(
        u.squares_to_bitboard(['a1', 'b3', 'c4', 'd5', 'e6', 'f7']))
    assert b.path_qubits("b3", "f7") == [bit_to_qubit(square_to_bit('c4')), \
                                         bit_to_qubit(square_to_bit('d5')), \
                                         bit_to_qubit(square_to_bit('e6'))]
    with pytest.raises(ValueError):
        b.path_qubits("a1", "b3")
    with pytest.raises(ValueError):
        b.path_qubits("c4", "a1")
예제 #11
0
def test_blocked_split_slide(board):
    b = qb.CirqBoard(u.squares_to_bitboard(['d1', 'g1']))
    assert b.perform_moves(
        'g1^e2h3:SPLIT_JUMP:BASIC',
        'd1^b3g4:SPLIT_SLIDE:BASIC',
    )
    test_utils.assert_sample_distribution(
        b, {
            u.squares_to_bitboard(['e2', 'b3']): 1 / 2,
            u.squares_to_bitboard(['h3', 'b3']): 1 / 4,
            u.squares_to_bitboard(['h3', 'g4']): 1 / 4,
        })
예제 #12
0
def test_path_qubits():
    """Source and target should be in the same line, otherwise ValueError should be returned."""
    b = qb.CirqBoard(
        u.squares_to_bitboard(["a1", "b3", "c4", "d5", "e6", "f7"]))
    assert b.path_qubits("b3", "f7") == [
        bit_to_qubit(square_to_bit("c4")),
        bit_to_qubit(square_to_bit("d5")),
        bit_to_qubit(square_to_bit("e6")),
    ]
    with pytest.raises(ValueError):
        b.path_qubits("a1", "b3")
    with pytest.raises(ValueError):
        b.path_qubits("c4", "a1")
예제 #13
0
def test_basic_ep():
    b = qb.CirqBoard(u.squares_to_bitboard(['b8', 'b5', 'c7']))
    assert b.perform_moves(
        'b8a6c6:SPLIT_JUMP:BASIC',
        'b5a6:PAWN_CAPTURE:BASIC',
        'c6b8:JUMP:BASIC',
        'c7c5:PAWN_TWO_STEP:BASIC',
        'b5c6:PAWN_EP:BASIC',
    )
    possibilities = [
        u.squares_to_bitboard(['b8', 'c6']),
        u.squares_to_bitboard(['a6', 'c5']),
    ]
    assert_samples_in(b, possibilities)
예제 #14
0
def test_a6(board):
    b = qb.CirqBoard(
        u.squares_to_bitboard(
            ['a2', 'f2', 'g2', 'h2', 'g3', 'c6', 'h6', 'f7', 'g7', 'h7', 'h8']))
    did_it_move = b.perform_moves(
        'a2a8:SLIDE:BASIC',
        'c6b8d8:SPLIT_JUMP:BASIC',
        'a8d8:SLIDE:CAPTURE',
    )
    if did_it_move:
        expected = u.squares_to_bitboard(
            ['d8', 'f2', 'g2', 'h2', 'g3', 'h6', 'f7', 'g7', 'h7', 'h8'])
    else:
        expected = u.squares_to_bitboard(
            ['a8', 'f2', 'g2', 'h2', 'g3', 'b8', 'h6', 'f7', 'g7', 'h7', 'h8'])
    assert_samples_in(b, [expected])
예제 #15
0
def test_undo_entangled_measurement():
    b = qb.CirqBoard(u.squares_to_bitboard(['a2', 'b1', 'c2', 'd1']))
    assert b.perform_moves('b1a3c3:SPLIT_JUMP:BASIC',
                           'c2c4:PAWN_TWO_STEP:BASIC')
    probs = b.get_probability_distribution(10000)
    assert_prob_about(probs, qb.square_to_bit('a3'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c2'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c3'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c4'), 0.5)
    b.perform_moves('d1c2:JUMP:EXCLUDED')
    assert b.undo_last_move()
    probs = b.get_probability_distribution(10000)
    assert_prob_about(probs, qb.square_to_bit('a3'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c2'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c3'), 0.5)
    assert_prob_about(probs, qb.square_to_bit('c4'), 0.5)
예제 #16
0
def test_capture_ep2():
    """Tests capture en passant.

    Splits b8 to a6/c6. Move c7 to c5 and perform en passant on c6.  This should
    either capture the knight or the pawn.
    """
    b = qb.CirqBoard(u.squares_to_bitboard(['b8', 'b5', 'c7']))
    did_it_move = b.perform_moves(
        'b8a6c6:SPLIT_JUMP:BASIC',
        'c7c5:PAWN_TWO_STEP:BASIC',
        'b5c6:PAWN_EP:CAPTURE',
    )
    assert did_it_move
    possibilities = [
        u.squares_to_bitboard(['c6', 'c7']),
        u.squares_to_bitboard(['c6', 'a6']),
    ]
    assert_samples_in(b, possibilities)
예제 #17
0
def test_illegal_castle(initial_board, source, target):
    """Tests various combinations of illegal capture.

    Args:
        initial_board: bitboard to set up
        source: king to move (should be e1 or e8)
        target: square to move king to.
    """
    b = qb.CirqBoard(initial_board)
    if target in ['g1', 'g8']:
        move_type = move_type = enums.MoveType.KS_CASTLE
    else:
        move_type = move_type = enums.MoveType.QS_CASTLE
    m = move.Move(source,
                  target,
                  move_type=move_type,
                  move_variant=enums.MoveVariant.BASIC)
    assert not b.do_move(m)
    assert_samples_in(b, [initial_board])
예제 #18
0
def test_blocked_slide_capture_through():
    success = 0
    for trials in range(100):
        b = qb.CirqBoard(u.squares_to_bitboard(['a8', 'c6']))
        b.do_move(
            move.Move('c6',
                      'b8',
                      target2='d8',
                      move_type=enums.MoveType.SPLIT_JUMP,
                      move_variant=enums.MoveVariant.BASIC))
        did_it_move = b.do_move(
            move.Move('a8',
                      'd8',
                      move_type=enums.MoveType.SLIDE,
                      move_variant=enums.MoveVariant.CAPTURE))
        if did_it_move:
            success += 1
    assert success > 25
    assert success < 75
예제 #19
0
def test_a9(board):
    b = qb.CirqBoard(
        u.squares_to_bitboard(
            ['h1', 'e2', 'f2', 'g2', 'e3', 'h3', 'g6', 'h6', 'c7']))
    did_it_move = b.perform_moves(
        'e2e3:SLIDE:CAPTURE',
        'c7^f4h2:SPLIT_SLIDE:BASIC',
        'e3h6:SLIDE:CAPTURE',
    )
    if did_it_move:
        possibilities = [
            u.squares_to_bitboard(['h1', 'f2', 'g2', 'h3', 'g6', 'h6', 'h2'])
        ]
    else:
        possibilities = [
            u.squares_to_bitboard(
                ['h1', 'f2', 'g2', 'e3', 'h3', 'g6', 'h6', 'f4'])
        ]
    assert_samples_in(b, possibilities)
예제 #20
0
def test_capture_ep():
    """Tests capture en passant.

    Splits b8 to a6/c6.  Capture a6 with b5 pawn to put the source pawn
    into superposition.

    Finally, move c7 to c5 and perform en passant on c6.
    """
    b = qb.CirqBoard(u.squares_to_bitboard(['b8', 'b5', 'c7']))
    did_it_move = b.perform_moves(
        'b8a6c6:SPLIT_JUMP:BASIC',
        'b5a6:PAWN_CAPTURE:BASIC',
        'c7c5:PAWN_TWO_STEP:BASIC',
        'b5c6:PAWN_EP:CAPTURE',
    )
    if did_it_move:
        expected = u.squares_to_bitboard(['c6', 'c7'])
    else:
        expected = u.squares_to_bitboard(['a6', 'c5'])
    assert_samples_in(b, [expected])
예제 #21
0
def test_blocked_ep():
    """Tests blocked en passant.

    Splits c4 to b6 and d6.  Moves a pawn through the piece on d6.
    Attempts to en passant the d-pawn using blocked e.p.
    """
    b = qb.CirqBoard(u.squares_to_bitboard(['c4', 'c5', 'd7']))
    did_it_move = b.perform_moves(
        'c4d6b6:SPLIT_JUMP:BASIC',
        'd7d5:PAWN_TWO_STEP:BASIC',
        'c5d6:PAWN_EP:EXCLUDED',
    )
    if did_it_move:
        possibilities = [
            u.squares_to_bitboard(['b6', 'd6']),
        ]
    else:
        possibilities = [
            u.squares_to_bitboard(['c5', 'd6', 'd7']),
        ]
    assert_samples_in(b, possibilities)
예제 #22
0
 def __init__(self,
              size: Optional[int] = 8,
              reps: Optional[int] = 1000,
              board=None):
     self._pieces = {}
     self._sampled = {}
     self.reps = reps
     self.board = board or qb.CirqBoard(0)
     for x in range(size):
         for y in range(size):
             s = to_square(x, y)
             self._pieces[s] = 0
     self.size = size
     self.ep_flag = None
     self.white_moves = True
     self.castling_flags = {
         'o-o': True,
         'o-o-o': True,
         'O-O': True,
         'O-O-O': True,
     }
예제 #23
0
def simulator(state):
    np.random.seed(get_seed())
    return qb.CirqBoard(state, error_mitigation=enums.ErrorMitigation.Error)
예제 #24
0
import recirq.quantum_chess.move as move
import recirq.quantum_chess.quantum_board as qb
from recirq.quantum_chess.test_utils import (
    assert_sample_distribution,
    assert_samples_in,
    assert_this_or_that,
    assert_prob_about,
    assert_fifty_fifty,
)
from recirq.quantum_chess.bit_utils import (
    bit_to_qubit,
    square_to_bit,
)

BIG_CIRQ_BOARDS = (
    qb.CirqBoard(0, error_mitigation=enums.ErrorMitigation.Error),
    qb.CirqBoard(0,
                 device=utils.get_device_obj_by_name('Syc54-noiseless'),
                 error_mitigation=enums.ErrorMitigation.Error),
)

ALL_CIRQ_BOARDS = BIG_CIRQ_BOARDS + (
    qb.CirqBoard(0,
                 device=utils.get_device_obj_by_name('Syc23-noiseless'),
                 error_mitigation=enums.ErrorMitigation.Error),
    qb.CirqBoard(0,
                 sampler=utils.get_sampler_by_name('Syc23-simulator-tester'),
                 device=utils.get_device_obj_by_name('Syc23-simulator-tester'),
                 error_mitigation=enums.ErrorMitigation.Correct,
                 noise_mitigation=0.10),
)
예제 #25
0
import recirq.quantum_chess.move as move
import recirq.quantum_chess.quantum_board as qb
import recirq.quantum_chess.bit_utils as u
from recirq.quantum_chess.test_utils import (
    assert_sample_distribution,
    assert_samples_in,
    assert_this_or_that,
    assert_prob_about,
    assert_fifty_fifty,
)

NOISY_SAMPLER = cirq.DensityMatrixSimulator(noise=ccn.DepolarizingNoiseModel(
    depol_prob=0.002))

BIG_CIRQ_BOARDS = (
    qb.CirqBoard(0, error_mitigation=enums.ErrorMitigation.Error),
    qb.CirqBoard(0,
                 device=cirq.google.Sycamore,
                 error_mitigation=enums.ErrorMitigation.Error),
)

ALL_CIRQ_BOARDS = BIG_CIRQ_BOARDS + (
    qb.CirqBoard(0,
                 device=cirq.google.Sycamore23,
                 error_mitigation=enums.ErrorMitigation.Error),
    qb.CirqBoard(0,
                 sampler=NOISY_SAMPLER,
                 device=cirq.google.Sycamore,
                 error_mitigation=enums.ErrorMitigation.Correct,
                 noise_mitigation=0.08),
)