Example #1
0
    def test_ansatz_circuit_two_layers(self, number_of_qubits, topology):
        # Given
        number_of_layers = 2
        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)),
        ]

        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_circuit = Circuit(expected_pycircuit)
        expected_circuit += get_entangling_layer(
            params[1], number_of_qubits, "XX", topology
        )

        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
 def test_get_executable_circuit_too_many_parameters(
         self, n_qubits, topology):
     # Given
     params = [
         np.random.rand(2 * n_qubits),
         np.random.rand(int((n_qubits * (n_qubits - 1)) / 2)),
         np.random.rand(2 * n_qubits),
     ]
     params = np.concatenate(params)
     ansatz = QCBMAnsatz(
         number_of_layers=2,
         number_of_qubits=n_qubits,
         topology=topology,
     )
     # When/Then
     with pytest.raises(ValueError):
         ansatz.get_executable_circuit(params),
    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
    def test_ansatz_circuit_one_layer(self, n_qubits, topology):
        # Given
        number_of_layers = 1
        ansatz = QCBMAnsatz(
            number_of_layers=number_of_layers,
            number_of_qubits=n_qubits,
            topology=topology,
        )

        params = [np.random.rand(n_qubits)]

        expected_circuit = Circuit()
        for i in range(n_qubits):
            expected_circuit += RX(params[0][i])(i)

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit
Example #6
0
    def test_ansatz_circuit_one_layer(self, number_of_qubits, topology):
        # Given
        number_of_layers = 1
        ansatz = QCBMAnsatz(
            number_of_layers=number_of_layers,
            number_of_qubits=number_of_qubits,
            topology=topology,
        )

        params = [np.ones(number_of_qubits)]

        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RX(params[0][i], i))
        expected_circuit = Circuit(expected_pycircuit)

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit
Example #7
0
    def test_ansatz_circuit_nine_layers(self, number_of_qubits, topology):
        # Given
        number_of_layers = 9
        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(2 * number_of_qubits),
            np.ones(int((number_of_qubits * (number_of_qubits - 1)) / 2)),
            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)),
            np.ones(2 * number_of_qubits),
        ]

        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)
            )
        expected_third_layer = Circuit(expected_pycircuit)
        expected_fourth_layer = get_entangling_layer(
            params[3], number_of_qubits, "XX", topology
        )
        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RX(params[4][i], i))
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RZ(params[4][i + number_of_qubits], i)
            )
        expected_fifth_layer = Circuit(expected_pycircuit)
        expected_sixth_layer = get_entangling_layer(
            params[5], number_of_qubits, "XX", topology
        )
        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RX(params[6][i], i))
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RZ(params[6][i + number_of_qubits], i)
            )
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RX(params[6][i + 2 * number_of_qubits], i)
            )
        expected_seventh_layer = Circuit(expected_pycircuit)
        expected_eigth_layer = get_entangling_layer(
            params[7], number_of_qubits, "XX", topology
        )
        expected_pycircuit = Program()
        for i in range(number_of_qubits):
            expected_pycircuit += Program(pyquil.gates.RZ(params[8][i], i))
        for i in range(number_of_qubits):
            expected_pycircuit += Program(
                pyquil.gates.RX(params[8][i + number_of_qubits], i)
            )
        expected_ninth_layer = Circuit(expected_pycircuit)
        expected_circuit = (
            expected_first_layer
            + expected_second_layer
            + expected_third_layer
            + expected_fourth_layer
            + expected_fifth_layer
            + expected_sixth_layer
            + expected_seventh_layer
            + expected_eigth_layer
            + expected_ninth_layer
        )

        params = np.concatenate(params)

        # When
        circuit = ansatz.get_executable_circuit(params)

        # Then
        assert circuit == expected_circuit