示例#1
0
def test_rand_unitary_haar_unitarity():
    """
    Random Qobjs: Tests that unitaries are actually unitary.
    """
    U = rand_unitary_haar(5)
    I = qeye(5)
    assert U * U.dag() == I
示例#2
0
    def test_N_level_system(self):
        """
        Test for circuit with N-level system.
        """
        mat3 = qp.rand_unitary_haar(3)

        def controlled_mat3(arg_value):
            """
            A qubit control an operator acting on a 3 level system
            """
            control_value = arg_value
            dim = mat3.dims[0][0]
            return (tensor(fock_dm(2, control_value), mat3) +
                    tensor(fock_dm(2, 1 - control_value), identity(dim)))

        qc = QubitCircuit(2, dims=[3, 2])
        qc.user_gates = {"CTRLMAT3": controlled_mat3}
        qc.add_gate("CTRLMAT3", targets=[1, 0], arg_value=1)
        props = qc.propagators()
        final_fid = qp.average_gate_fidelity(mat3, ptrace(props[0], 0) - 1)
        assert pytest.approx(final_fid, 1.0e-6) == 1

        init_state = basis([3, 2], [0, 1])
        result = qc.run(init_state)
        final_fid = qp.fidelity(result, props[0] * init_state)
        assert pytest.approx(final_fid, 1.0e-6) == 1.
示例#3
0
def h_sho(dim):
    U = rand_unitary_haar(dim)
    H = U * (num(dim) + 0.5) * U.dag()
    H = H / np.linalg.norm(H.full(), ord=2)
    return H
示例#4
0
 def test_unitaries_equal_1(self, dimension):
     """Tests that for random unitaries U, AGF(U, U) = 1."""
     tol = 1e-7
     U = rand_unitary_haar(dimension)
     SU = to_super(U)
     assert average_gate_fidelity(SU, target=U) == pytest.approx(1, abs=tol)
示例#5
0
def random_unitary(steps, d):
    for _ in range(steps):
        q.rand_unitary_haar(d)
示例#6
0
    return scheme


#def recast_scheme(coeffs, rho, obs):
#    en = [o.eigenenergies() for o in obs]
#    en_min

# =========================================================================== #
# Some testing of the functions
# =========================================================================== #
# test methods: vectorization / experimental observables/ schmidt decompo
assert np.allclose(
    lqo2arr(arr2lqo(np.eye(16))),
    np.eye(16)), "Pb with the (de)casting of list of qobj to matrices"
test_dim = 4
test_obs, test_rho, test_A, test_ket = qutip.rand_herm(
    test_dim), qutip.rand_dm(test_dim), qutip.rand_unitary_haar(
        test_dim), qutip.rand_ket(test_dim)
test_mat = np.random.normal(size=[test_dim, test_dim])
assert np.abs(
    get_exp_obs(test_A, test_rho, test_obs) -
    get_exp_obs(test_A, test_rho, test_obs, 1000000)
) < 1e-3, "for that many obs estimated val and th val should be the same"
assert np.abs(
    get_exp_obs(test_A, test_rho, test_obs) -
    get_exp_obs(test_A, test_rho, test_obs, 100)) > 1e-3
assert np.allclose(devect(vect(test_mat)),
                   test_mat), "Pb with (de)vectorization"
assert is_close(schm_recompo(schm_decompo(test_ket)),
                test_ket), "Schm decompo/recompo don't work"
            #M = A.dot(np.transpose(C))
            # aasert(M, np.eye(len(s)))
            return (np.ones(len(s)), p, s)
    
    return scheme
#def recast_scheme(coeffs, rho, obs):
#    en = [o.eigenenergies() for o in obs]
#    en_min

# =========================================================================== #
# Some testing of the functions
# =========================================================================== #
# test methods: vectorization / experimental observables/ schmidt decompo
assert np.allclose(lqo2arr(arr2lqo(np.eye(16))), np.eye(16)), "Pb with the (de)casting of list of qobj to matrices"
test_dim = 4
test_obs, test_rho, test_A, test_ket = qutip.rand_herm(test_dim), qutip.rand_dm(test_dim), qutip.rand_unitary_haar(test_dim), qutip.rand_ket(test_dim)
test_mat = np.random.normal(size=[test_dim, test_dim])
assert np.abs(get_exp_obs(test_A, test_rho, test_obs) - get_exp_obs(test_A, test_rho, test_obs, 1000000)) <1e-3, "for that many obs estimated val and th val should be the same"
assert np.abs(get_exp_obs(test_A, test_rho, test_obs) - get_exp_obs(test_A, test_rho, test_obs, 100)) > 1e-3
assert np.allclose(devect(vect(test_mat)), test_mat), "Pb with (de)vectorization"
assert is_close(schm_recompo(schm_decompo(test_ket)), test_ket), "Schm decompo/recompo don't work"

# =========================================================================== #
# Testing the different measurement schemes
# =========================================================================== #
U = unitary_group.rvs(4, 100)
u = [Qobj(U_el, dims = [[2,2],[2,2]]) for U_el in U]
real_fid = [fid_perfect(unit, CN) for unit in u]

### NIELSEN INITIAL
res_nielsen = produce_schemes_2q()(CN)
B = qt.tensor(zero*zero.dag(), X) + qt.tensor(one*one.dag(), Z)
C = qt.tensor(plus*plus.dag(), X) + qt.tensor(minus*minus.dag(), Z)
D = qt.tensor(plus*plus.dag(), Z) + qt.tensor(minus*minus.dag(), X)






###########
# Optimize observables basis
###########
##### State to look at 
#1q stuff
q1_pauli = [I, X, Y, Z]
q1_haar = qt.rand_unitary_haar(2, [[2],[2]]) * zero
q1_haar2 = qt.rand_unitary_haar(2, [[2],[2]]) * zero
q1_haar3 = qt.rand_unitary_haar(2, [[2],[2]]) * zero

#2q stuff
q2_sep = qt.tensor(zero, one)
q2_sep_haar = qt.tensor(*[q1_haar, q1_haar2])
q2_haar = qt.rand_unitary_haar(4, [[2,2],[2,2]]) * q2_sep
q2_ghz = 1/np.sqrt(2) * (qt.tensor(zero, zero) + qt.tensor(one, one))
q2_W = 1/np.sqrt(2) * (qt.tensor(zero, one) + qt.tensor(one, zero))

#3q stuff
q3_sep = qt.tensor(zero, one, one)
q3_sep_haar = qt.tensor(*[q1_haar,q1_haar2,q1_haar3])
q3_haar = qt.rand_unitary_haar(8, [[2,2,2],[2,2,2]]) * q3_sep
q3_ghz = 1/np.sqrt(2) * (qt.tensor(zero, zero, zero) + qt.tensor(one, one, one))
示例#9
0
def random_unitary(steps, d):
    for _ in range(steps):
        q.rand_unitary_haar(d)