Beispiel #1
0
    def test_get_entangling_layer_topology_supported(self):
        # Given
        n_qubits_list = [2, 3, 4, 5]
        static_entangler = XX
        topology = "all"

        for n_qubits in n_qubits_list:
            # Given
            params = np.zeros((int((n_qubits * (n_qubits - 1)) / 2)))
            all_topology_layer = get_entangling_layer_all_topology(
                params, n_qubits, static_entangler
            )
            # When
            entangling_layer = get_entangling_layer(
                params, n_qubits, static_entangler, topology
            )
            # Then
            assert all_topology_layer == entangling_layer

        # Given
        topology = "line"

        for n_qubits in n_qubits_list:
            # Given
            params = np.zeros((n_qubits - 1))
            line_topology_layer = get_entangling_layer_line_topology(
                params, n_qubits, static_entangler
            )
            # When
            entangling_layer = get_entangling_layer(
                params, n_qubits, static_entangler, topology
            )
            # Then
            assert line_topology_layer == entangling_layer
    def test_ansatz_circuit_nine_layers(self, n_qubits, topology):
        # Given
        number_of_layers = 9
        ansatz = QCBMAnsatz(
            number_of_layers=number_of_layers,
            number_of_qubits=n_qubits,
            topology=topology,
        )
        params = [
            np.random.rand(2 * n_qubits),
            np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
            np.random.rand(2 * n_qubits),
            np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
            np.random.rand(2 * n_qubits),
            np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
            np.random.rand(3 * n_qubits),
            np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
            np.random.rand(2 * n_qubits),
        ]
        expected_circuit = Circuit([
            # First layer
            *[RX(params[0][i])(i) for i in range(n_qubits)],
            *[RZ(params[0][i + n_qubits])(i) for i in range(n_qubits)],
            # Second layer
            *get_entangling_layer(params[1], n_qubits, XX,
                                  topology).operations,
            # Third layer
            *[RX(params[2][i])(i) for i in range(n_qubits)],
            *[RZ(params[2][i + n_qubits])(i) for i in range(n_qubits)],
            # Fouth layer
            *get_entangling_layer(params[3], n_qubits, XX,
                                  topology).operations,
            # Fifth layer
            *[RX(params[4][i])(i) for i in range(n_qubits)],
            *[RZ(params[4][i + n_qubits])(i) for i in range(n_qubits)],
            # Sixth layer
            *get_entangling_layer(params[5], n_qubits, XX,
                                  topology).operations,
            # Seventh layer
            *[RX(params[6][i])(i) for i in range(n_qubits)],
            *[RZ(params[6][i + n_qubits])(i) for i in range(n_qubits)],
            *[RX(params[6][i + 2 * n_qubits])(i) for i in range(n_qubits)],
            # Eigth layer
            *get_entangling_layer(params[7], n_qubits, XX,
                                  topology).operations,
            # Ningth layer
            *[RZ(params[8][i])(i) for i in range(n_qubits)],
            *[RX(params[8][i + n_qubits])(i) for i in range(n_qubits)],
        ])

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit
Beispiel #3
0
    def test_ansatz_circuit_four_layers(self, number_of_qubits, topology):
        # Given
        number_of_layers = 4
        ansatz = QCBMAnsatz(
            number_of_layers=number_of_layers,
            number_of_qubits=number_of_qubits,
            topology=topology,
        )

        params = [
            np.ones(2 * number_of_qubits),
            np.ones(int((number_of_qubits * (number_of_qubits - 1)) / 2)),
            np.ones(3 * number_of_qubits),
            np.ones(int((number_of_qubits * (number_of_qubits - 1)) / 2)),
        ]

        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RX(params[0][i], i))
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RZ(params[0][i + number_of_qubits], i)
            )
        expected_first_layer = Circuit(expected_pycircuit)
        expected_second_layer = get_entangling_layer(
            params[1], number_of_qubits, "XX", topology
        )
        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RX(params[2][i], i))
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RZ(params[2][i + number_of_qubits], i)
            )
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RX(params[2][i + 2 * number_of_qubits], i)
            )
        expected_third_layer = Circuit(expected_pycircuit)
        expected_fourth_layer = get_entangling_layer(
            params[3], number_of_qubits, "XX", topology
        )
        expected_circuit = (
            expected_first_layer
            + expected_second_layer
            + expected_third_layer
            + expected_fourth_layer
        )

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit
    def test_ansatz_circuit_two_layers(self, n_qubits, topology):
        # Given
        number_of_layers = 2
        ansatz = QCBMAnsatz(
            number_of_layers=number_of_layers,
            number_of_qubits=n_qubits,
            topology=topology,
        )

        params = [
            np.random.rand(2 * n_qubits),
            np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
        ]

        expected_circuit = Circuit([
            # First layer
            *[RX(params[0][i])(i) for i in range(n_qubits)],
            *[RZ(params[0][i + n_qubits])(i) for i in range(n_qubits)],
            # Second layer
            *get_entangling_layer(params[1], n_qubits, XX,
                                  topology).operations,
        ])

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit
Beispiel #5
0
    def test_get_entangling_layer_toplogy_not_supported(self):
        # Given
        n_qubits = 2
        static_entangler = XX
        topology = "NOT SUPPORTED"
        params = np.zeros(1)

        # When
        with pytest.raises(RuntimeError):
            _ = get_entangling_layer(params, n_qubits, static_entangler, topology)
Beispiel #6
0
    def test_get_entangling_layer_graph_topology_wrong_matrix(
        self, n_qubits, matrix, n_connections
    ):
        # Given
        single_qubit_gate = RX
        static_entangler = XX
        topology = "graph"

        params = np.asarray([0] * n_connections)
        # When
        with pytest.raises(Exception):
            _ = get_entangling_layer(
                params, n_qubits, static_entangler, topology, matrix
            )
Beispiel #7
0
    def test_get_entangling_layers_fails_with_incorrect_graph_kwargs(self):
        # Given
        n_qubits = 4
        single_qubit_gate = RX
        static_entangler = XX
        topology = "graph"
        connectivity = np.asarray(
            [[1, 1, 0, 1], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
        )
        connectivity2 = np.asarray([[[0, 1], [2, 3], [0, 3]]])
        params = np.asarray([0, 0, 0])

        with pytest.raises(TypeError):
            _ = get_entangling_layer(
                params,
                n_qubits,
                static_entangler,
                topology,
                connectivity,
                connectivity2,
            )
Beispiel #8
0
 def test_default_star_qubit(self, n_qubits):
     default_layer = get_entangling_layer(
         np.zeros(n_qubits - 1), n_qubits, XX, "star"
     )
     for i in range(n_qubits - 1):
         assert default_layer.operations[i].qubit_indices == (0, i + 1)