def test_supports_observable_argument_types(self): """Checks that device.supports_observable returns the correct result when passed both string and Operation class arguments""" self.logTestName() mock_device = Device() self.assertTrue(mock_device.supports_observable('PauliX')) self.assertTrue(mock_device.supports_observable(qml.PauliX))
def mock_device_with_capabilities(): """A mock instance of the abstract Device class with non-empty observables""" with patch.multiple(Device, __abstractmethods__=set(), _capabilities=mock_device_capabilities): yield Device()
def mock_device_supporting_paulis(monkeypatch): """A mock instance of the abstract Device class with non-empty observables""" with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, 'operations', mock_device_paulis) m.setattr(Device, 'observables', mock_device_paulis) m.setattr(Device, 'short_name', 'MockDevice') yield Device()
def mock_device_with_observables(): """A mock instance of the abstract Device class with non-empty observables""" with patch.multiple( Device, __abstractmethods__=set(), observables=PropertyMock(return_value=["PauliX", "PauliZ"]), ): yield Device()
def mock_device_supporting_observables_and_inverse(monkeypatch): """A mock instance of the abstract Device class with non-empty operations and supporting inverses""" with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, 'operations', mock_device_paulis) m.setattr(Device, 'observables', mock_device_paulis + ['Hermitian']) m.setattr(Device, 'short_name', 'MockDevice') m.setattr(Device, '_capabilities', {"inverse_operations": True}) yield Device()
def mock_device_supporting_paulis(): """A mock instance of the abstract Device class with non-empty observables""" with patch.multiple( Device, __abstractmethods__=set(), operations=PropertyMock(return_value=mock_device_paulis), observables=PropertyMock(return_value=mock_device_paulis), short_name=PropertyMock(return_value="MockDevice"), ): yield Device()
def mock_device(monkeypatch): with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, '_capabilities', mock_device_capabilities) m.setattr(Device, 'operations', ["PauliY", "RX", "Rot"]) m.setattr(Device, 'observables', ["PauliZ"]) m.setattr(Device, 'short_name', 'MockDevice') m.setattr(Device, 'expval', lambda self, x, y, z: 0) m.setattr(Device, 'var', lambda self, x, y, z: 0) m.setattr(Device, 'sample', lambda self, x, y, z: 0) m.setattr(Device, 'apply', lambda self, x, y, z: None) yield Device()
def mock_device(self): with patch.multiple( Device, __abstractmethods__=set(), operations=PropertyMock(return_value=["PauliY", "RX", "Rot"]), observables=PropertyMock(return_value=["PauliZ"]), short_name=PropertyMock(return_value="MockDevice"), expval=MagicMock(return_value=0), var=MagicMock(return_value=0), sample=MagicMock(return_value=[0]), apply=MagicMock()): yield Device()
def mock_device_with_paulis_and_methods(monkeypatch): """A mock instance of the abstract Device class with non-empty observables""" with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, '_capabilities', mock_device_capabilities) m.setattr(Device, 'operations', mock_device_paulis) m.setattr(Device, 'observables', mock_device_paulis) m.setattr(Device, 'short_name', 'MockDevice') m.setattr(Device, 'expval', lambda self, x, y, z: 0) m.setattr(Device, 'var', lambda self, x, y, z: 0) m.setattr(Device, 'sample', lambda self, x, y, z: 0) m.setattr(Device, 'apply', lambda self, x, y, z: None) yield Device()
def mock_device_with_paulis_and_methods(): """A mock instance of the abstract Device class with non-empty observables""" with patch.multiple( Device, __abstractmethods__=set(), _capabilities=mock_device_capabilities, expval=MagicMock(return_value=0), var=MagicMock(return_value=0), sample=MagicMock(return_value=[0]), apply=MagicMock(), operations=PropertyMock(return_value=mock_device_paulis), observables=PropertyMock(return_value=mock_device_paulis), short_name=PropertyMock(return_value="MockDevice"), ): yield Device()
def test_supports_operation_exception(self): """check that a the function device.supports_operation raises proper errors if the argument is of the wrong type""" self.logTestName() mock_device = Device() with self.assertRaisesRegex( ValueError, "The given operation must either be a pennylane.Operation class or a string." ): mock_device.supports_operation(3) with self.assertRaisesRegex( ValueError, "The given operation must either be a pennylane.Operation class or a string." ): mock_device.supports_operation(Device)
def mock_device_with_capabilities(monkeypatch): """A mock instance of the abstract Device class with non-empty observables""" with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, '_capabilities', mock_device_capabilities) yield Device()
def mock_device_with_operations(monkeypatch): """A mock instance of the abstract Device class with non-empty operations""" with monkeypatch.context() as m: m.setattr(Device, '__abstractmethods__', frozenset()) m.setattr(Device, 'operations', ["PauliX", "PauliZ", "CNOT"]) yield Device()
def get_device(wires=1): return Device(wires=wires)
def test_args(self, mock_device): """Test that the device requires correct arguments""" with pytest.raises( qml.DeviceError, match="specified number of shots needs to be at least 1"): Device(mock_device, shots=0)
def mock_device(): """A mock instance of the abstract Device class""" with patch.multiple(Device, __abstractmethods__=set()): yield Device()