Exemple #1
0
 def test_failure_if_non_arraylike_coordinates(self, xs):
     state = qutip.rand_ket(4)
     valid = np.linspace(-1, 1, 5)
     with pytest.raises(TypeError) as e:
         qutip.qfunc(state, xs, valid)
     assert "must be array-like" in e.value.args[0]
     with pytest.raises(TypeError) as e:
         qutip.qfunc(state, valid, xs)
     assert "must be array-like" in e.value.args[0]
     with pytest.raises(TypeError) as e:
         qutip.QFunc(xs, valid)
     assert "must be array-like" in e.value.args[0]
     with pytest.raises(TypeError) as e:
         qutip.QFunc(valid, xs)
     assert "must be array-like" in e.value.args[0]
Exemple #2
0
 def test_QFunc_raises_if_insufficient_memory(self):
     xs = np.linspace(-1, 1, 11)
     state = qutip.rand_ket(4)
     qfunc = qutip.QFunc(xs, xs, memory=0)
     with pytest.raises(MemoryError) as e:
         qfunc(state)
     assert e.value.args[0].startswith("Refusing to precompute")
Exemple #3
0
 def test_failure_if_coordinates_not_1d(self, ndim):
     state = qutip.rand_ket(4)
     valid = np.linspace(-1, 1, 5)
     bad = valid.reshape((-1, ) + (1, ) * (ndim - 1))
     with pytest.raises(ValueError) as e:
         qutip.qfunc(state, bad, valid)
     assert "must be 1D" in e.value.args[0]
     with pytest.raises(ValueError) as e:
         qutip.qfunc(state, valid, bad)
     assert "must be 1D" in e.value.args[0]
     with pytest.raises(ValueError) as e:
         qutip.QFunc(bad, valid)
     assert "must be 1D" in e.value.args[0]
     with pytest.raises(ValueError) as e:
         qutip.QFunc(valid, bad)
     assert "must be 1D" in e.value.args[0]
Exemple #4
0
 def test_function_and_class_are_equivalent(self, size, dm, n_xs, n_ys, g):
     xs = np.linspace(-1, 1, n_xs)
     ys = np.linspace(0, 2, n_ys)
     state = qutip.rand_dm(size) if dm else qutip.rand_ket(size)
     function = qutip.qfunc(state, xs, ys, g)
     class_ = qutip.QFunc(xs, ys, g)(state)
     np.testing.assert_allclose(function, class_)
Exemple #5
0
 def test_qfunc_is_linear(self, n_xs, n_ys, mix):
     xs = np.linspace(-1, 1, n_xs)
     ys = np.linspace(-1, 1, n_ys)
     qfunc = qutip.QFunc(xs, ys)
     left, right = qutip.rand_dm(5), qutip.rand_dm(5)
     qleft, qright = qfunc(left), qfunc(right)
     qboth = qfunc(mix * left + (1 - mix) * right)
     np.testing.assert_allclose(mix * qleft + (1 - mix) * qright, qboth)
Exemple #6
0
 def test_failure_if_not_a_Qobj(self, obj):
     xs = np.linspace(-1, 1, 11)
     with pytest.raises(TypeError) as e:
         qutip.qfunc(obj, xs, xs)
     assert e.value.args[0].startswith("state must be Qobj")
     qfunc = qutip.QFunc(xs, xs)
     with pytest.raises(TypeError) as e:
         qfunc(obj)
     assert e.value.args[0].startswith("state must be Qobj")
Exemple #7
0
 def test_same_class_can_take_many_sizes(self, dm, initial_size):
     xs = np.linspace(-1, 1, 11)
     ys = np.linspace(0, 2, 11)
     shape = np.meshgrid(xs, ys)[0].shape
     sizes = initial_size + np.array([0, 1, -1, 4])
     qfunc = qutip.QFunc(xs, ys)
     for size in sizes:
         state = qutip.rand_dm(size) if dm else qutip.rand_ket(size)
         out = qfunc(state)
         assert isinstance(out, np.ndarray)
         assert out.shape == shape
Exemple #8
0
 def test_failure_if_tensor_hilbert_space(self, dm):
     if dm:
         state = qutip.rand_dm(4, dims=[[2, 2], [2, 2]])
     else:
         state = qutip.rand_ket(4, dims=[[2, 2], [1, 1]])
     xs = np.linspace(-1, 1, 5)
     with pytest.raises(ValueError) as e:
         qutip.qfunc(state, xs, xs)
     assert "must not have tensor structure" in e.value.args[0]
     with pytest.raises(ValueError) as e:
         qutip.QFunc(xs, xs)(state)
     assert "must not have tensor structure" in e.value.args[0]
Exemple #9
0
 def test_against_naive_implementation(self, xs, ys, g, size):
     state = qutip.rand_dm(size)
     state_np = state.full()
     x, y = np.meshgrid(xs, ys)
     alphas = 0.5 * g * (x + 1j * y)
     naive = np.empty(alphas.shape, dtype=np.float64)
     for i, alpha in enumerate(alphas.flat):
         coh = qutip.coherent(size, alpha, method='analytic').full()
         naive.flat[i] = (coh.conj().T @ state_np @ coh).real
     naive *= (0.5 * g)**2 / np.pi
     np.testing.assert_allclose(naive, qutip.qfunc(state, xs, ys, g))
     np.testing.assert_allclose(naive, qutip.QFunc(xs, ys, g)(state))
Exemple #10
0
 def test_failure_if_not_a_state(self, state):
     xs = np.linspace(-1, 1, 11)
     state = state()
     with pytest.raises(ValueError) as e:
         qutip.qfunc(state, xs, xs)
     assert (e.value.args[0].startswith(
         "state must be a ket or density matrix"))
     qfunc = qutip.QFunc(xs, xs)
     with pytest.raises(ValueError) as e:
         qfunc(state)
     assert (e.value.args[0].startswith(
         "state must be a ket or density matrix"))
Exemple #11
0
 def test_same_class_can_mix_ket_and_dm(self, dm_first):
     dms = [True, False, True, False]
     if not dm_first:
         dms = dms[::-1]
     xs = np.linspace(-1, 1, 11)
     ys = np.linspace(0, 2, 11)
     shape = np.meshgrid(xs, ys)[0].shape
     qfunc = qutip.QFunc(xs, ys)
     for dm in dms:
         state = qutip.rand_dm(4) if dm else qutip.rand_ket(4)
         out = qfunc(state)
         assert isinstance(out, np.ndarray)
         assert out.shape == shape
Exemple #12
0
 def test_ket_and_dm_give_same_result(self, n_xs, n_ys, size):
     xs = np.linspace(-1, 1, n_xs)
     ys = np.linspace(-1, 1, n_ys)
     state = qutip.rand_ket(size)
     qfunc = qutip.QFunc(xs, ys)
     np.testing.assert_allclose(qfunc(state), qfunc(state.proj()))