def test_closed_workers_not_reused(self): qubit_operator = QubitOperator('X0') parallel_qubit_op = ParallelLinearQubitOperator( qubit_operator, 1, options=LinearQubitOperatorOptions(processes=2)) state = [1.0, 0.0] parallel_qubit_op.dot(state) parallel_qubit_op.dot(state) self.assertIsNone(parallel_qubit_op.options.pool)
class LinearQubitOperatorOptionsTest(unittest.TestCase): """Tests for LinearQubitOperatorOptions class.""" def setUp(self): """LinearQubitOperatorOptions test set up.""" self.processes = multiprocessing.cpu_count() self.options = LinearQubitOperatorOptions(self.processes) def test_init(self): """Tests __init__().""" self.assertEqual(self.options.processes, self.processes) self.assertIsNone(self.options.pool) def test_get_processes_small(self): """Tests get_processes() with a small num.""" num = 1 self.assertEqual(self.options.get_processes(num), num) def test_get_processes_large(self): """Tests get_processes() with a large num.""" self.assertEqual(self.options.get_processes(2*self.processes), self.processes) def test_invalid_processes(self): """Tests with invalid processes since it's not positive.""" with self.assertRaises(ValueError): LinearQubitOperatorOptions(0) def test_get_pool(self): """Tests get_pool() without a num.""" self.assertIsNone(self.options.pool) pool = self.options.get_pool() self.assertIsNotNone(pool) def test_get_pool_with_num(self): """Tests get_processes() with a num.""" self.assertIsNone(self.options.pool) pool = self.options.get_pool(2) self.assertIsNotNone(pool)
class LinearQubitOperatorOptionsTest(unittest.TestCase): """Tests for LinearQubitOperatorOptions class.""" def setUp(self): """LinearQubitOperatorOptions test set up.""" self.processes = 6 self.options = LinearQubitOperatorOptions(self.processes) def test_init(self): """Tests __init__().""" self.assertEqual(self.options.processes, self.processes) self.assertIsNone(self.options.pool) def test_get_processes_small(self): """Tests get_processes() with a small num.""" num = 2 self.assertEqual(self.options.get_processes(num), num) def test_get_processes_large(self): """Tests get_processes() with a large num.""" self.assertEqual(self.options.get_processes(20), self.processes) def test_invalid_processes(self): """Tests with invalid processes since it's not positive.""" with self.assertRaises(ValueError): LinearQubitOperatorOptions(0) def test_get_pool(self): """Tests get_pool() without a num.""" self.assertIsNone(self.options.pool) pool = self.options.get_pool() self.assertIsNotNone(pool) self.assertIsNotNone(self.options.pool) self.assertEqual(pool, self.options.pool) def test_get_pool_with_num(self): """Tests get_processes() with a num.""" self.assertIsNone(self.options.pool) pool = self.options.get_pool(2) self.assertIsNotNone(pool) self.assertIsNotNone(self.options.pool) self.assertEqual(pool, self.options.pool) # Called twice, should be idempotent. self.assertEqual(self.options.get_pool(2), self.options.pool) # Same pool even with a different number of processes. self.assertEqual(self.options.get_pool(1), self.options.pool)
def test_generate_linear_operator(self): """Tests generate_linear_qubit_operator().""" qubit_operator = (QubitOperator('Z3') + QubitOperator('X1') + QubitOperator('Y0')) n_qubits = 6 # Checks types. operator = generate_linear_qubit_operator(qubit_operator, n_qubits) self.assertTrue(isinstance(operator, LinearQubitOperator)) self.assertFalse(isinstance(operator, ParallelLinearQubitOperator)) operator_again = generate_linear_qubit_operator( qubit_operator, n_qubits, options=LinearQubitOperatorOptions(2)) self.assertTrue(isinstance(operator_again, ParallelLinearQubitOperator)) self.assertFalse(isinstance(operator_again, LinearQubitOperator)) # Checks operators are equivalent. numpy.random.seed(n_qubits) vec = numpy.random.rand(2 ** n_qubits, 1) self.assertTrue(numpy.allclose(operator * vec, operator_again * vec))
def test_invalid_processes(self): """Tests with invalid processes since it's not positive.""" with self.assertRaises(ValueError): LinearQubitOperatorOptions(0)
def setUp(self): """LinearQubitOperatorOptions test set up.""" self.processes = multiprocessing.cpu_count() self.options = LinearQubitOperatorOptions(self.processes)
def setUp(self): """LinearQubitOperatorOptions test set up.""" self.processes = 6 self.options = LinearQubitOperatorOptions(self.processes)