def test_expval(self): dev = ProjectQIBMBackend(wires=2, use_hardware=False, num_runs=8 * 1024, user='******', password='******', verbose=True) dev._eng = MagicMock() dev._eng.backend = MagicMock() dev._eng.backend.get_probabilities = MagicMock() dev._eng.backend.get_probabilities.return_value = { '00': 0.1, '01': 0.3, '10': 0.2, '11': 0.4 } self.assertAlmostEqual(dev.expval('PauliZ', wires=[0], par=list()), -0.2, delta=self.tol) self.assertAlmostEqual(dev.expval('Identity', wires=[0], par=list()), 1.0, delta=self.tol) self.assertRaises(NotImplementedError, dev.expval, 'Hermitian', wires=[0], par=list())
def test_expval_exception_if_no_expval_queue(self): if self.args.device == 'ibm' or self.args.device == 'all': dev = ProjectQIBMBackend(wires=2, shots=1, use_hardware=False, user='******', password='******', verbose=True) else: return del dev.__dict__['_expval_queue'] dev._eng = MagicMock() dev._eng.backend = MagicMock() dev._eng.backend.get_probabilities = MagicMock() dev._eng.backend.get_probabilities.return_value = {'00': 1.0} self.assertRaises(DeviceError, dev.expval, 'PauliX', wires=[0], par=list()) self.assertRaises(DeviceError, dev.expval, 'PauliY', wires=[0], par=list()) self.assertRaises(DeviceError, dev.expval, 'Hadamard', wires=[0], par=list())
def setUp(self): super().setUp() self.devices = [] if self.args.device == 'simulator' or self.args.device == 'all': self.devices.append(ProjectQSimulator(wires=self.num_subsystems)) self.devices.append( ProjectQSimulator(wires=self.num_subsystems, shots=20000000)) if self.args.device == 'ibm' or self.args.device == 'all': ibm_options = pennylane.default_config['projectq.ibm'] if "user" in ibm_options and "password" in ibm_options: self.devices.append( ProjectQIBMBackend(wires=self.num_subsystems, use_hardware=False, num_runs=8 * 1024, user=ibm_options['user'], password=ibm_options['password'])) else: self.devices.append( ProjectQIBMBackend(wires=self.num_subsystems, use_hardware=False, num_runs=8 * 1024, user='******', password='******')) if self.args.device == 'classical' or self.args.device == 'all': self.devices.append( ProjectQClassicalSimulator(wires=self.num_subsystems))
def test_shots(self): if self.args.device == 'ibm' or self.args.device == 'all': shots = 5 dev1 = ProjectQIBMBackend(wires=self.num_subsystems, shots=shots, use_hardware=False, user="******", password='******') self.assertEqual(shots, dev1.shots) dev2 = ProjectQIBMBackend(wires=self.num_subsystems, num_runs=shots, use_hardware=False, user="******", password='******') self.assertEqual(shots, dev2.shots) dev2 = ProjectQIBMBackend(wires=self.num_subsystems, shots=shots+2, num_runs=shots, use_hardware=False, user="******", password='******') self.assertEqual(shots, dev2.shots)
def test_shots(self): if self.args.device == 'ibm' or self.args.device == 'all': shots = 5 dev1 = ProjectQIBMBackend(wires=self.num_subsystems, shots=shots, use_hardware=False, token=token, verbose=True) self.assertEqual(shots, dev1.shots) dev2 = ProjectQIBMBackend(wires=self.num_subsystems, num_runs=shots, use_hardware=False, token=token) self.assertEqual(shots, dev2.shots) dev2 = ProjectQIBMBackend(wires=self.num_subsystems, shots=shots+2, num_runs=shots, use_hardware=False, token=token) self.assertEqual(shots, dev2.shots)
def setUp(self): super().setUp() self.devices = [] if self.args.device == 'simulator' or self.args.device == 'all': self.devices.append( ProjectQSimulator(wires=self.num_subsystems, verbose=True)) if self.args.device == 'ibm' or self.args.device == 'all': ibm_options = pennylane.default_config['projectq.ibm'] if "user" in ibm_options and "password" in ibm_options: self.devices.append( ProjectQIBMBackend(wires=self.num_subsystems, use_hardware=False, num_runs=8 * 1024, user=ibm_options['user'], password=ibm_options['password'], verbose=True)) else: log.warning( "Skipping test of the ProjectQIBMBackend device because IBM login credentials could not be found in the PennyLane configuration file." ) if self.args.device == 'classical' or self.args.device == 'all': self.devices.append( ProjectQClassicalSimulator(wires=self.num_subsystems, verbose=True))
def setUp(self): super().setUp() self.devices = [] if self.args.device == 'simulator' or self.args.device == 'all': self.devices.append(ProjectQSimulator(wires=self.num_subsystems)) self.devices.append( ProjectQSimulator(wires=self.num_subsystems, shots=20000000)) if self.args.device == 'ibm' or self.args.device == 'all': self.devices.append( ProjectQIBMBackend(wires=self.num_subsystems, use_hardware=False, num_runs=8 * 1024, token=token, verbose=True)) if self.args.device == 'classical' or self.args.device == 'all': self.devices.append( ProjectQClassicalSimulator(wires=self.num_subsystems))
def test_pre_expval(self): with patch('pennylane_pq.devices.ProjectQIBMBackend.expval_queue', new_callable=PropertyMock) as mock_expval_queue: mock_PauliX = MagicMock(wires=[0]) mock_PauliX.name = 'PauliX' mock_PauliY = MagicMock(wires=[0]) mock_PauliY.name = 'PauliY' mock_Hadamard = MagicMock(wires=[0]) mock_Hadamard.name = 'Hadamard' mock_Hermitian = MagicMock(wires=[0]) mock_Hermitian.name = 'Hermitian' mock_expval_queue.return_value = [ mock_PauliX, mock_PauliY, mock_Hadamard, ] dev = ProjectQIBMBackend(wires=2, use_hardware=False, num_runs=8 * 1024, user='******', password='******', verbose=True) dev._eng = MagicMock() dev.apply = MagicMock() with patch('projectq.ops.All', new_callable=PropertyMock) as mock_All: dev.pre_expval() dev._eng.assert_has_calls([call.flush()]) # The following might have to be changed in case a more elegant/efficient/different # implementation of the effective measurements is found dev.apply.assert_has_calls([ call('Hadamard', [0], []), call('PauliZ', [0], []), call('S', [0], []), call('Hadamard', [0], []), call('RY', [0], [-0.7853981633974483]) ]) mock_expval_queue.return_value = [mock_Hermitian] with patch('projectq.ops.All', new_callable=PropertyMock) as mock_All: self.assertRaises(NotImplementedError, dev.pre_expval)