def test_replace_error_message(self): """Test that an exception is raised when replacing layers in an uninitialized Grid is attempted.""" grid = Grid() with pytest.raises( AttributeError, match= "Can't replace layer. The Grid has not yet been initialized."): grid.replace_layer(1, [1, 2, 3])
def test_append_wire(self): """Test that wire appending works properly.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) grid.append_wire([6, 7]) assert np.array_equal(grid.raw_grid, [[0, 3], [1, 4], [2, 5], [6, 7]]) assert np.array_equal(grid.raw_grid.T, [[0, 1, 2, 6], [3, 4, 5, 7]])
def test_insert_wire(self, idx, expected_grid): """Test that wire insertion works properly.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) grid.insert_wire(idx, [6, 7]) assert np.array_equal(grid.raw_grid, expected_grid) assert np.array_equal(grid.raw_grid.T, _transpose(expected_grid))
def test_replace_layer(self, idx, expected_transposed_grid): """Test that layer replacement works properly.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) grid.replace_layer(idx, [6, 7, 8]) assert np.array_equal(grid.raw_grid.T, expected_transposed_grid) assert np.array_equal(grid.raw_grid, _transpose(expected_transposed_grid))
def test_append_layer(self): """Test that layer appending works properly.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) grid.append_layer([6, 7, 8]) assert np.array_equal(grid.raw_grid, [[0, 3, 6], [1, 4, 7], [2, 5, 8]]) assert np.array_equal(grid.raw_grid.T, [[0, 1, 2], [3, 4, 5], [6, 7, 8]])
def test_copy(self): """Test that copy copies the grid.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) other_grid = grid.copy() # Assert that everything is indeed copied assert other_grid is not grid assert other_grid.raw_grid is not grid.raw_grid # Assert the copy is correct assert np.array_equal(other_grid.raw_grid, grid.raw_grid)
def test_append_grid_by_layers(self): """Test appending a grid to another by layers.""" raw_grid_transpose1 = [[0, 3], [1, 4], [2, 5]] raw_grid_transpose2 = [[6, 7], [8, 9]] grid1 = Grid(_transpose(raw_grid_transpose1)) grid2 = Grid(_transpose(raw_grid_transpose2)) grid1.append_grid_by_layers(grid2) assert np.array_equal(grid1.raw_grid.T, [[0, 3], [1, 4], [2, 5], [6, 7], [8, 9]]) assert np.array_equal( grid1.raw_grid, _transpose([[0, 3], [1, 4], [2, 5], [6, 7], [8, 9]]))
def test_empty_init(self): """Test that the Grid class is initialized correctly when no raw_grid is given.""" grid = Grid() assert grid.num_layers == 0 assert grid.num_wires == 0 assert grid.raw_grid is None
def to_grid(layer_list, num_wires): """Convert the given list of operations per layer to a Grid. Args: layer_list (list[list[~.Operation]]): List of layers in terms of Operations num_wires (int): Number of wires in the system Returns: ~.Grid: The corresponding Operation grid """ grid = Grid(_transpose([to_layer(layer_list[0], num_wires)])) for i in range(1, len(layer_list)): grid.append_layer(to_layer(layer_list[i], num_wires)) return grid
def test_empty(self): """Tests creating a grid with empty operations and no wire map""" grid = drawable_grid([]) assert grid == [[]] Grid_obj = Grid(grid) assert Grid_obj.raw_grid.shape == (1, 0)
def test_resolve_representation(self, dummy_circuit_drawer): """Test that resolve_representation calls the representation resolver with the proper arguments.""" dummy_circuit_drawer.representation_resolver.element_representation = Mock( return_value="Test" ) dummy_circuit_drawer.resolve_representation(Grid(dummy_raw_operation_grid), Grid()) args_tuples = [ call[0] for call in dummy_circuit_drawer.representation_resolver.element_representation.call_args_list ] for idx, wire in enumerate(dummy_raw_operation_grid): for op in wire: assert (op, idx) in args_tuples
def test_init(self): """Test that the Grid class is initialized correctly.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) assert np.array_equal(grid.raw_grid, raw_grid) assert np.array_equal(grid.raw_grid.T, [[0, 1, 2], [3, 4, 5]])
def test_error_init_with_wrong_grid_shape(self, raw_grid): """Test that the Grid class is initialized correctly.""" with pytest.raises( ValueError, match= "The entered raw grid was not parsed as two-dimensional array" ): grid = Grid(raw_grid)
def test_empty_wire_map(self): """Test creating a grid when no operations are present but there is a wire map""" wire_map = {1: 1, 2: 2} grid = drawable_grid([], wire_map) assert grid == [[], []] Grid_obj = Grid(grid) assert Grid_obj.raw_grid.shape == (2, 0)
def test_single_wires_blocking(self): """Test where single wire gates block each other""" ops = [qml.PauliX(0), qml.PauliX(0), qml.PauliX(0)] grid = drawable_grid(ops) assert grid == [ops] Grid_obj = Grid(grid) assert Grid_obj.raw_grid.shape == (1, 3)
def test_single_wires_no_blocking(self): """Test where nothing blocks each other""" ops = [qml.PauliX(0), qml.PauliX(1), qml.PauliX(2)] grid = drawable_grid(ops) assert grid == [[ops[0]], [ops[1]], [ops[2]]] Grid_obj = Grid(grid) assert Grid_obj.raw_grid.shape == (3, 1)
def test_resolve_decorations(self, grid, target_representation_grid): """Test that decorations are properly resolved.""" representation_grid = Grid() raw_operator_grid = grid.raw_grid # make a dummy observable grid raw_observable_grid = [[None] for _ in range(len(raw_operator_grid))] drawer = CircuitDrawer(raw_operator_grid, raw_observable_grid, Wires(range(10))) drawer.resolve_decorations(grid, representation_grid) assert_nested_lists_equal(representation_grid.raw_grid, target_representation_grid.raw_grid)
def test_multiwire_blocking(self): """Test multi-wire gate blocks on unused wire.""" wire_map = {0: 0, 1: 1, 2: 2} ops = [qml.PauliZ(1), qml.CNOT(wires=(0, 2)), qml.PauliX(1)] grid = drawable_grid(ops, wire_map) assert grid[0] == [None, ops[1], None] assert grid[1] == [ops[0], None, ops[2]] assert grid[2] == [None, ops[1], None] Grid_obj = Grid(grid) assert Grid_obj.raw_grid.shape == (3, 3)
class TestCircuitDrawer: """Test the CircuitDrawer class.""" def test_resolve_representation(self, dummy_circuit_drawer): """Test that resolve_representation calls the representation resolver with the proper arguments.""" dummy_circuit_drawer.representation_resolver.element_representation = Mock( return_value="Test" ) dummy_circuit_drawer.resolve_representation(Grid(dummy_raw_operation_grid), Grid()) args_tuples = [ call[0] for call in dummy_circuit_drawer.representation_resolver.element_representation.call_args_list ] for idx, wire in enumerate(dummy_raw_operation_grid): for op in wire: assert (op, idx) in args_tuples interlocking_multiwire_gate_grid = to_grid( [[qml.CNOT(wires=[0, 4]), qml.CNOT(wires=[1, 5]), qml.Toffoli(wires=[2, 3, 6])]], 7 ) interlocking_multiwire_gate_representation_grid = Grid( [ ["╭", "", ""], ["│", "╭", ""], ["│", "│", "╭"], ["│", "│", "├"], ["╰", "│", "│"], ["", "╰", "│"], ["", "", "╰"], ] ) multiwire_and_single_wire_gate_grid = to_grid( [[qml.Toffoli(wires=[0, 3, 4]), qml.PauliX(wires=[1]), qml.Hadamard(wires=[2])]], 5 ) multiwire_and_single_wire_gate_representation_grid = Grid([["╭"], ["│"], ["│"], ["├"], ["╰"]]) all_wire_state_preparation_grid = to_grid( [[qml.BasisState(np.array([0, 1, 0, 0, 1, 1]), wires=[0, 1, 2, 3, 4, 5])]], 6 ) all_wire_state_preparation_representation_grid = Grid( [["╭"], ["├"], ["├"], ["├"], ["├"], ["╰"]] ) multiwire_gate_grid = to_grid( [[qml.CNOT(wires=[0, 1]), qml.PauliX(2), qml.CNOT(wires=[3, 4])]], 5 ) multiwire_gate_representation_grid = Grid( [ ["╭"], ["╰"], [""], ["╭"], ["╰"], ] ) multi_and_single_wire_gate_grid = to_grid( [ [ qml.CNOT(wires=[0, 1]), qml.PauliX(2), qml.PauliX(4), qml.CNOT(wires=[3, 5]), qml.Hadamard(6), ] ], 7, ) multi_and_single_wire_gate_representation_grid = Grid( [ ["╭"], ["╰"], [""], ["╭"], ["│"], ["╰"], [""], ] ) @pytest.mark.parametrize( "grid,target_representation_grid", [ (interlocking_multiwire_gate_grid, interlocking_multiwire_gate_representation_grid), ( multiwire_and_single_wire_gate_grid, multiwire_and_single_wire_gate_representation_grid, ), (all_wire_state_preparation_grid, all_wire_state_preparation_representation_grid), (multiwire_gate_grid, multiwire_gate_representation_grid), (multi_and_single_wire_gate_grid, multi_and_single_wire_gate_representation_grid), ], ) def test_resolve_decorations(self, grid, target_representation_grid): """Test that decorations are properly resolved.""" representation_grid = Grid() raw_operator_grid = grid.raw_grid # make a dummy observable grid raw_observable_grid = [[None] for _ in range(len(raw_operator_grid))] drawer = CircuitDrawer(raw_operator_grid, raw_observable_grid, Wires(range(10))) drawer.resolve_decorations(grid, representation_grid) assert_nested_lists_equal(representation_grid.raw_grid, target_representation_grid.raw_grid) CNOT04 = qml.CNOT(wires=[0, 4]) CNOT15 = qml.CNOT(wires=[1, 5]) Toffoli236 = qml.Toffoli(wires=[2, 3, 6]) interlocking_CNOT_grid = to_grid([[CNOT04, CNOT15, Toffoli236]], 7) moved_interlocking_CNOT_grid = to_grid([[Toffoli236], [CNOT15], [CNOT04]], 7) SWAP02 = qml.SWAP(wires=[0, 2]) SWAP35 = qml.SWAP(wires=[3, 5]) SWAP14 = qml.SWAP(wires=[1, 4]) SWAP24 = qml.SWAP(wires=[2, 4]) interlocking_SWAP_grid = to_grid([[SWAP02, SWAP35, SWAP14], [SWAP24]], 6) moved_interlocking_SWAP_grid = to_grid([[SWAP35], [SWAP14], [SWAP02], [SWAP24]], 6) @pytest.mark.parametrize( "grid,target_grid", [ (interlocking_CNOT_grid, moved_interlocking_CNOT_grid), (interlocking_SWAP_grid, moved_interlocking_SWAP_grid), ], ) def test_move_multi_wire_gates(self, grid, target_grid): """Test that decorations are properly resolved.""" operator_grid = grid.copy() raw_operator_grid = operator_grid.raw_grid # make a dummy observable grid raw_observable_grid = [[None] for _ in range(len(raw_operator_grid))] drawer = CircuitDrawer(raw_operator_grid, raw_observable_grid, Wires(range(10))) drawer.move_multi_wire_gates(operator_grid) assert_nested_lists_equal(operator_grid.raw_grid, target_grid.raw_grid)
def test_num_wires(self, raw_grid, expected_num_wires): """Test that num_layers returns the correct number of wires.""" grid = Grid(raw_grid) assert grid.num_wires == expected_num_wires
def test_layer(self, raw_transposed_grid): """Test that layer returns the correct layer.""" grid = Grid(_transpose(raw_transposed_grid)) for idx, layer in enumerate(raw_transposed_grid): assert np.array_equal(grid.layer(idx), layer)
def test_str(self): """Test string rendering of Grid.""" raw_grid = [[0, 3], [1, 4], [2, 5]] grid = Grid(raw_grid) assert str(grid) == "[0 3]\n[1 4]\n[2 5]\n"
def test_wire(self, raw_grid): """Test that wire returns the correct wire.""" grid = Grid(raw_grid) for idx, wire in enumerate(raw_grid): assert np.array_equal(grid.wire(idx), wire)