예제 #1
0
    def test_apply_parametrized_operation_inverse(self, op, pars, wires, expected_circuit):
        """Tests that inverse operations get recognized and converted to correct parameters for
        parametrized ops."""

        dev = AQTDevice(2, api_key=SOME_API_KEY)
        dev._apply_operation(op(*pars, wires=wires).inv())

        assert dev.circuit == expected_circuit
예제 #2
0
    def test_unsupported_operation_exception(self):
        """Tests whether an exception is raised if an unsupported operation
        is attempted to be appended to queue."""

        dev = AQTDevice(1, api_key=SOME_API_KEY)

        with pytest.raises(qml.DeviceError, match="is not supported on AQT devices"):
            dev._append_op_to_queue("BAD_GATE", 0.5, [0])
예제 #3
0
    def test_apply_basisstate_not_first_exception(self):
        """Tests that the apply method raises an exception when BasisState
        is not the first operation."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)

        with pytest.raises(qml.DeviceError, match="only supported at the beginning of a circuit"):
            dev.apply([qml.RX(0.5, wires=1), qml.BasisState(np.array([1, 1, 1]), wires=[0, 1, 2])])
예제 #4
0
    def test_apply_qubitstatevector_not_first_exception(self):
        """Tests that the apply method raises an exception when QubitStateVector
        is not the first operation."""

        dev = AQTDevice(2, api_key=SOME_API_KEY)

        state = np.ones(8) / np.sqrt(8)
        with pytest.raises(qml.DeviceError, match="only supported at the beginning of a circuit"):
            dev.apply([qml.RX(0.5, wires=1), qml.QubitStateVector(state, wires=[0, 1, 2])])
예제 #5
0
    def test_apply_operation_pauli(self, wires, op, aqt_name):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a PennyLane Pauli operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(op(wires=wires))

        assert dev.circuit == [[aqt_name, 1.0, wires]]
예제 #6
0
    def test_apply_operation_S(self, wires):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a PennyLane S operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(qml.S(wires=wires))

        assert dev.circuit == [["Z", 0.5, wires]]
예제 #7
0
    def test_apply_operation_hadamard(self, wires):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a PennyLane Hadamard operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(qml.Hadamard(wires=wires))

        assert dev.circuit == [["X", 1.0, wires], ["Y", -0.5, wires]]
예제 #8
0
    def test_apply_operation_R(self, wires, par0, par1):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a R gate operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(ops.R(par0, par1, wires=wires))

        assert dev.circuit == [["R", par0, par1, wires]]
예제 #9
0
    def test_apply_operation_rotations(self, op, par, wires, aqt_name):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a PennyLane RX, RY, or RZ operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(op(par, wires=wires))
        aqt_par = par / np.pi

        assert dev.circuit == [[aqt_name, aqt_par, wires]]
예제 #10
0
    def test_retry_delay(self):
        """Tests that the ``retry_delay`` property can be set manually."""

        dev = AQTDevice(3, api_key=SOME_API_KEY, retry_delay=2.5)
        assert dev.retry_delay == 2.5

        dev.retry_delay = 1.0
        assert dev.retry_delay == 1.0

        with pytest.raises(qml.DeviceError, match="needs to be positive"):
            dev.retry_delay = -5
예제 #11
0
    def test_generate_samples(self, samples, indices):
        """Tests that the generate_samples function of AQTDevice provides samples in
        the correct format expected by PennyLane."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        dev.shots = 10
        dev.samples = samples
        res = dev.generate_samples()
        expected_array = np.stack([np.ravel(indices)] * 10)

        assert res.shape == (dev.shots, dev.num_wires)
        assert np.all(res == expected_array)
예제 #12
0
    def test_apply_operation_basisstate(self, wires, state):
        """Tests that the _apply_operation method correctly populates the circuit
        queue when a PennyLane BasisState operation is provided."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev._apply_operation(qml.BasisState(np.array(state), wires=wires))
        expected_circuit = []
        for bit, wire in zip(state, wires):
            if bit == 1:
                expected_circuit.append(["X", 1.0, [wire]])

        assert dev.circuit == expected_circuit
예제 #13
0
    def test_api_key_not_found_error(self, monkeypatch, tmpdir):
        """Tests that an error is thrown with the device is created without
        a valid API token."""

        monkeypatch.setenv("AQT_TOKEN", "")
        monkeypatch.setenv("PENNYLANE_CONF", "")
        monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file"))

        monkeypatch.setattr(
            "pennylane.default_config", qml.Configuration("config.toml")
        )  # force loading of config
        with pytest.raises(ValueError, match="No valid api key for AQT platform found"):
            dev = AQTDevice(2)
예제 #14
0
    def test_set_api_configs(self):
        """Tests that the ``set_api_configs`` method properly (re)sets the API configs."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        new_api_key = "ZZZ000"
        dev._api_key = new_api_key
        dev.BASE_HOSTNAME = "https://server.someaddress.com"
        dev.TARGET_PATH = "some/path"
        dev.set_api_configs()

        assert dev.header == {"Ocp-Apim-Subscription-Key": new_api_key, "SDK": "pennylane"}
        assert dev.data == {"access_token": new_api_key, "no_qubits": dev.num_wires}
        assert dev.hostname == "https://server.someaddress.com/some/path"
예제 #15
0
    def test_reset(self):
        """Tests that the ``reset`` method corretly resets data."""

        dev = AQTDevice(3, api_key=SOME_API_KEY)
        assert dev.circuit == []

        dev.circuit = [["RX", 0.5, [0]]]
        dev.circuit_json = "some dummy string"
        dev.samples = [5, 5, 5]
        dev.shots = 55

        dev.reset()

        assert dev.circuit == []
        assert dev.circuit_json == ""
        assert dev.samples == None
        assert dev.shots == 55  # should not be reset
예제 #16
0
    def test_default_init(self, num_wires, shots, retry_delay):
        """Tests that the device is properly initialized."""

        dev = AQTDevice(num_wires, shots, SOME_API_KEY, retry_delay)

        assert dev.num_wires == num_wires
        assert dev.shots == shots
        assert dev.retry_delay == retry_delay
        assert dev.analytic == False
        assert dev.circuit == []
        assert dev.circuit_json == ""
        assert dev.samples is None
        assert dev.BASE_HOSTNAME == BASE_HOSTNAME
        assert dev.HTTP_METHOD == HTTP_METHOD
        assert API_HEADER_KEY in dev.header.keys()
        assert dev.header[API_HEADER_KEY] == SOME_API_KEY
예제 #17
0
 def test_serialize(self, circuit, expected):
     """Tests that the ``serialize`` static method correctly converts
     from a list of lists into an acceptable JSON string."""
     dev = AQTDevice(3, api_key=SOME_API_KEY)
     res = dev.serialize(circuit)
     assert res == expected