def test_sprepost(self):
        U1 = rand_unitary(3)
        U2 = rand_unitary(3)

        S1 = spre(U1) * spost(U2)
        S2 = sprepost(U1, U2)

        assert_(S1 == S2)
    def test_sprepost(self):
        U1 = rand_unitary(3)
        U2 = rand_unitary(3)

        S1 = spre(U1) * spost(U2)
        S2 = sprepost(U1, U2)

        assert_(S1 == S2)
def unitary_channel(dimension,desired_fidelity,error):
    noise_operator = qt.to_super(qt.rand_unitary(dimension))
    count=0
    while np.abs(qt.average_gate_fidelity(noise_operator)-desired_fidelity)>error and count<400:
        noise_operator = qt.to_super(qt.rand_unitary(dimension))
        count=count+1
    if count>=400: 
        print("Could not achieve desired fidelity within margin of error")
    return noise_operator 
Beispiel #4
0
    def testRandUnitarySeed(self):
        "random Unitary with seed"

        seed = 12345
        U0 = rand_unitary(5, seed=seed)
        U1 = rand_unitary(5, seed=None)
        U2 = rand_unitary(5, seed=seed)
        assert_(U0 != U1)
        assert_(U0 == U2)
Beispiel #5
0
    def testRandUnitarySeed(self):
        "random Unitary with seed"

        seed = 12345
        U0 = rand_unitary(5, seed=seed)
        U1 = rand_unitary(5, seed=None)
        U2 = rand_unitary(5, seed=seed)
        assert_(U0 != U1)
        assert_(U0 == U2)
    def test_reshuffle(self):
        U1 = rand_unitary(2)
        U2 = rand_unitary(3)
        U3 = rand_unitary(4)

        U = tensor(U1, U2, U3)
        S = to_super(U)
        S_col = reshuffle(S)

        assert_equal(S_col.dims[0], [[2, 2], [3, 3], [4, 4]])

        assert_(reshuffle(S_col) == S)
    def test_reshuffle(self):
        U1 = rand_unitary(2)
        U2 = rand_unitary(3)
        U3 = rand_unitary(4)

        U = tensor(U1, U2, U3)
        S = to_super(U)
        S_col = reshuffle(S)

        assert_equal(S_col.dims[0], [[2, 2], [3, 3], [4, 4]])

        assert_(reshuffle(S_col) == S)
Beispiel #8
0
def initialise(no_inputs, ancilla, bond_dimension,identity=0):
    """generates the right number of random unitaries of the right size"""
    total=no_inputs+ancilla
    operator_list=[]
    temp_operator_list=[]
    for i in range(no_inputs):
        if identity==1:
            operator_list.append(np.identity(bond_dimension).astype(complex))
        elif identity==2:
            tt=qutip.rand_unitary(bond_dimension).full()
            operator_list.append(np.dot(tt,tt*1J))
        else:
            operator_list.append(qutip.rand_unitary(bond_dimension).full().astype(complex))
    
    return operator_list
Beispiel #9
0
 def test_invariant_under_unitary_transformation(self, dimension):
     rho1 = rand_dm(dimension, 0.25)
     rho2 = rand_dm(dimension, 0.25)
     U = rand_unitary(dimension, 0.25)
     D = tracedist(rho1, rho2)
     DU = tracedist(U * rho1 * U.dag(), U * rho2 * U.dag())
     assert D == pytest.approx(DU, rel=1e-5)
Beispiel #10
0
 def test_permutation_without_expansion(self, permutation):
     base = qutip.tensor([qutip.rand_unitary(2) for _ in permutation])
     test = gates.expand_operator(base,
                                  N=len(permutation),
                                  targets=permutation)
     expected = base.permute(_apply_permutation(permutation))
     np.testing.assert_allclose(test.full(), expected.full(), atol=1e-15)
Beispiel #11
0
 def new_game(self):
     self.unitary = qt.rand_unitary(2**self.n_players)
     self.player_colors = [[random.uniform(0,1), random.uniform(0,1), random.uniform(0,1)] for i in range(self.n_players)]
     self.current_state = qt.basis(self.unitary.shape[0], 0)
     self.desired_state = self.unitary*self.current_state
     self.winning = False
     self.goal_stars = q_SurfaceXYZ(self.desired_state)
Beispiel #12
0
def test_DenseValsOnly():
    """
    Dense eigvals only Hermitian
    """
    H = rand_herm(10)
    spvals = H.eigenenergies(sparse=False)
    assert_equal(len(spvals), 10)
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] <= spvals[-1], True)
    # check that spvals equal expect vals
    for k in range(10):
        # check that ouput is real for Hermitian operator
        assert_equal(np.isreal(spvals[k]), True)
    spvals = H.eigenenergies(sparse=False, sort='high')
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] >= spvals[-1], True)
    spvals = H.eigenenergies(sparse=False, sort='high', eigvals=4)
    assert_equal(len(spvals), 4)

    U = rand_unitary(10)
    spvals = U.eigenenergies(sparse=False)
    assert_equal(len(spvals), 10)
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] <= spvals[-1], True)
    # check that spvals equal expect vals
    for k in range(10):
        # check that ouput is real for Hermitian operator
        assert_equal(np.iscomplex(spvals[k]), True)
    spvals = U.eigenenergies(sparse=False, sort='high')
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] >= spvals[-1], True)
    spvals = U.eigenenergies(sparse=False, sort='high', eigvals=4)
    assert_equal(len(spvals), 4)
Beispiel #13
0
def test_DenseValsOnly():
    """
    Dense eigvals only Hermitian
    """
    H = rand_herm(10)
    spvals = H.eigenenergies(sparse=False)
    assert_equal(len(spvals), 10)
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] <= spvals[-1], True)
    # check that spvals equal expect vals
    for k in range(10):
        # check that ouput is real for Hermitian operator
        assert_equal(np.isreal(spvals[k]), True)
    spvals = H.eigenenergies(sparse=False, sort='high')
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] >= spvals[-1], True)
    spvals = H.eigenenergies(sparse=False, sort='high', eigvals=4)
    assert_equal(len(spvals), 4)

    U = rand_unitary(10)
    spvals = U.eigenenergies(sparse=False)
    assert_equal(len(spvals), 10)
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] <= spvals[-1], True)
    # check that spvals equal expect vals
    for k in range(10):
        # check that ouput is real for Hermitian operator
        assert_equal(np.iscomplex(spvals[k]), True)
    spvals = U.eigenenergies(sparse=False, sort='high')
    # check that sorting is lowest eigval first
    assert_equal(spvals[0] >= spvals[-1], True)
    spvals = U.eigenenergies(sparse=False, sort='high', eigvals=4)
    assert_equal(len(spvals), 4)
Beispiel #14
0
 def test_invariant_under_unitary_transformation(self, dimension):
     rho1 = rand_dm(dimension, 0.25)
     rho2 = rand_dm(dimension, 0.25)
     U = rand_unitary(dimension, 0.25)
     F = fidelity(rho1, rho2)
     FU = fidelity(U * rho1 * U.dag(), U * rho2 * U.dag())
     assert F == pytest.approx(FU, rel=1e-5)
Beispiel #15
0
def _make_random_three_qubit_gate():
    """Create a random three-qubit gate."""
    operation = qutip.rand_unitary(8, dims=[[2]*3]*2)

    def gate(N=None, controls=None, target=None):
        if N is None:
            return operation
        return gates.gate_expand_3toN(operation, N, controls, target)
    return gate
 def rand_rotate_hol(self):
     '''Description: rotates evolved states and operators by 
     random unitaries (one for each item in a list)'''
     randunitary = qt.rand_unitary(2, density=0.75)
     self.rand_rotate_op_list = [randunitary * i for i in self.op_list]
     self.rand_rotate_state_list = [
         randunitary * i for i in self.state_list
     ]
     self.rand_rotate_Uj_list = [randunitary * i for i in self.Uj_list_hol]
     return self.rand_rotate_op_list, self.rand_rotate_state_list, self.rand_rotate_Uj_list
Beispiel #17
0
 def test_general_qubit_expansion(self, n_targets):
     # Test all permutations with the given number of targets.
     n_qubits = 5
     operation = qutip.rand_unitary(2**n_targets, dims=[[2]*n_targets]*2)
     for targets in itertools.permutations(range(n_qubits), n_targets):
         expected = _tensor_with_entanglement([qutip.qeye(2)] * n_qubits,
                                              operation, targets)
         test = gates.expand_operator(operation, n_qubits, targets)
         np.testing.assert_allclose(test.full(), expected.full(),
                                    atol=1e-15)
Beispiel #18
0
def test_SparseValsVecs():
    """
    Sparse eigs non-Hermitian
    """
    U = rand_unitary(10)
    spvals, spvecs = U.eigenstates(sparse=True)
    assert_equal(np.real(spvals[0]) <= np.real(spvals[-1]), True)
    for k in range(10):
        # check that eigenvectors are right and in right order
        assert_equal(abs(expect(U, spvecs[k]) - spvals[k]) < 5e-14, True)
        assert_equal(np.iscomplex(spvals[k]), True)

    # check sorting
    spvals, spvecs = U.eigenstates(sparse=True, sort='high')
    assert_equal(np.real(spvals[0]) >= np.real(spvals[-1]), True)

    # check for N-1 eigenvals
    U = rand_unitary(10)
    spvals, spvecs = U.eigenstates(sparse=True, eigvals=9)
    assert_equal(len(spvals), 9)
Beispiel #19
0
def test_SparseValsVecs():
    """
    Sparse eigs non-Hermitian
    """
    U = rand_unitary(10)
    spvals, spvecs = U.eigenstates(sparse=True)
    assert_equal(np.real(spvals[0]) <= np.real(spvals[-1]), True)
    for k in range(10):
        # check that eigenvectors are right and in right order
        assert_equal(abs(expect(U, spvecs[k]) - spvals[k]) < 5e-14, True)
        assert_equal(np.iscomplex(spvals[k]), True)

    # check sorting
    spvals, spvecs = U.eigenstates(sparse=True, sort='high')
    assert_equal(np.real(spvals[0]) >= np.real(spvals[-1]), True)

    # check for N-1 eigenvals
    U = rand_unitary(10)
    spvals, spvecs = U.eigenstates(sparse=True, eigvals=9)
    assert_equal(len(spvals), 9)
Beispiel #20
0
 def test_non_qubit_systems(self, dimensions):
     n_qubits = len(dimensions)
     for targets in itertools.permutations(range(n_qubits), 2):
         operators = [qutip.rand_unitary(dimension) if n in targets
                      else qutip.qeye(dimension)
                      for n, dimension in enumerate(dimensions)]
         expected = qutip.tensor(*operators)
         base_test = qutip.tensor(*[operators[x] for x in targets])
         test = gates.expand_operator(base_test, N=n_qubits,
                                      targets=targets, dims=dimensions)
         assert test.dims == expected.dims
         np.testing.assert_allclose(test.full(), expected.full())
Beispiel #21
0
    def testOperatorUnitaryTransform(self):
        """
        Superoperator: Unitary transformation with operators and superoperators
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = U * rho * U.dag()
        rho2_vec = spre(U) * spost(U.dag()) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
Beispiel #22
0
    def testOperatorSpostAppl(self):
        """
        Superoperator: apply operator and superoperator from right (spost)
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = rho * U
        rho2_vec = spost(U) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
Beispiel #23
0
    def testOperatorSpreAppl(self):
        """
        Superoperator: apply operator and superoperator from left (spre)
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = U * rho
        rho2_vec = spre(U) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
Beispiel #24
0
 def __init__(self, players):
     self.n_players = players
     self.unitary = qt.rand_unitary(2**self.n_players)
     #self.unitary = (-2*math.pi*1j*0.001*qt.rand_herm(2**self.n_players)).expm()
     self.current_players = []
     self.player_colors = [[random.uniform(0,1), random.uniform(0,1), random.uniform(0,1)] for i in range(self.n_players)]
     self.current_state = qt.basis(self.unitary.shape[0], 0)
     self.desired_state = self.unitary*self.current_state
     #self.current_state = self.desired_state
     #self.current_history = []
     self.winning = False
     self.goal_stars = q_SurfaceXYZ(self.desired_state)
     self.ai_on = False
Beispiel #25
0
def test_simdiag(num_mat):
    N = 10

    U = qutip.rand_unitary(N)
    commuting_matrices = [
        U * qutip.qdiags(np.random.rand(N), 0) * U.dag()
        for _ in range(num_mat)
    ]
    all_evals, evecs = qutip.simdiag(commuting_matrices)

    for matrix, evals in zip(commuting_matrices, all_evals):
        for eval, evec in zip(evals, evecs):
            assert matrix * evec == evec * eval
Beispiel #26
0
def test_simdiag_degen_large():
    N = 20
    U = qutip.rand_unitary(N)
    commuting_matrices = [
        U * qutip.qdiags(np.random.randint(0, 3, N), 0) * U.dag()
        for _ in range(5)
    ]
    all_evals, evecs = qutip.simdiag(commuting_matrices, tol=1e-12)

    for matrix, evals in zip(commuting_matrices, all_evals):
        for eval, evec in zip(evals, evecs):
            np.testing.assert_allclose((matrix * evec).full(),
                                       (evec * eval).full())
    def testOperatorUnitaryTransform(self):
        """
        Superoperator: Unitary transformation with operators and superoperators
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = U * rho * U.dag()
        rho2_vec = spre(U) * spost(U.dag()) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
    def testOperatorSpostAppl(self):
        """
        Superoperator: apply operator and superoperator from right (spost)
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = rho * U
        rho2_vec = spost(U) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
    def testOperatorSpreAppl(self):
        """
        Superoperator: apply operator and superoperator from left (spre)
        """
        N = 3
        rho = rand_dm(N)
        U = rand_unitary(N)

        rho1 = U * rho
        rho2_vec = spre(U) * operator_to_vector(rho)
        rho2 = vector_to_operator(rho2_vec)

        assert_((rho1 - rho2).norm() < 1e-8)
Beispiel #30
0
def test_simdiag_degen():
    N = 10
    U = qutip.rand_unitary(N)
    commuting_matrices = [
        U * qutip.qdiags([0, 0, 0, 1, 1, 1, 2, 2, 3, 4], 0) * U.dag(),
        U * qutip.qdiags([0, 0, 0, 1, 2, 2, 2, 2, 2, 2], 0) * U.dag(),
        U * qutip.qdiags([0, 0, 2, 1, 1, 2, 2, 3, 3, 4], 0) * U.dag(),
    ]
    all_evals, evecs = qutip.simdiag(commuting_matrices)

    for matrix, evals in zip(commuting_matrices, all_evals):
        for eval, evec in zip(evals, evecs):
            np.testing.assert_allclose((matrix * evec).full(),
                                       (evec * eval).full())
Beispiel #31
0
def test_simdiag_no_evals(num_mat):
    N = 10

    U = qutip.rand_unitary(N)
    commuting_matrices = [
        U * qutip.qdiags(np.random.rand(N), 0) * U.dag()
        for _ in range(num_mat)
    ]
    evecs = qutip.simdiag(commuting_matrices, evals=False)

    for matrix in commuting_matrices:
        for evec in evecs:
            Mvec = matrix * evec
            eval = Mvec.norm() / evec.norm()
            assert matrix * evec == evec * eval
Beispiel #32
0
def test_DenseValsVecs():
    """
    Dense eigs non-Hermitian
    """
    W = rand_herm(10,0.5) + 1j*rand_herm(10,0.5)
    spvals, spvecs = W.eigenstates(sparse=False)
    assert_equal(np.real(spvals[0]) <= np.real(spvals[-1]), True)
    for k in range(10):
        # check that eigenvectors are right and in right order
        assert_equal(abs(expect(W, spvecs[k]) - spvals[k]) < 1e-14, True)
        assert_(np.iscomplex(spvals[k]))

    # check sorting
    spvals, spvecs = W.eigenstates(sparse=False, sort='high')
    assert_equal(np.real(spvals[0]) >= np.real(spvals[-1]), True)

    # check for N-1 eigenvals
    W = rand_unitary(10)
    spvals, spvecs = W.eigenstates(sparse=False, eigvals=9)
    assert_equal(len(spvals), 9)
Beispiel #33
0
def test_DenseValsVecs():
    """
    Dense eigs non-Hermitian
    """
    W = rand_herm(10,0.5) + 1j*rand_herm(10,0.5)
    spvals, spvecs = W.eigenstates(sparse=False)
    assert_equal(np.real(spvals[0]) <= np.real(spvals[-1]), True)
    for k in range(10):
        # check that eigenvectors are right and in right order
        assert_equal(abs(expect(W, spvecs[k]) - spvals[k]) < 1e-14, True)
        assert_(np.iscomplex(spvals[k]))

    # check sorting
    spvals, spvecs = W.eigenstates(sparse=False, sort='high')
    assert_equal(np.real(spvals[0]) >= np.real(spvals[-1]), True)

    # check for N-1 eigenvals
    W = rand_unitary(10)
    spvals, spvecs = W.eigenstates(sparse=False, eigvals=9)
    assert_equal(len(spvals), 9)
        else:
            for pr in combinations(list(range(N)), n):
                s = reduce(lambda x, y: x * y, [m[p] for p in pr])
                terms.append(op[c] * s)
                terms[-1].dims = m[0].dims
                c += 1
    return sum(terms)


#########################################################################################

n_ = int(n / 2)
f_ = fermion_operators(n_)
m_ = reduce(lambda x, y: x + y, majorana_operators(f_))

O = qt.rand_unitary(2**n_)
O.dims = [[2] * n_, [2] * n_]

OL = from_majorana_basis(to_majorana_basis(O, m_), mL)
OR = from_majorana_basis(to_majorana_basis(O, m_), [1j * r for r in mR])


def size(O, i=None):
    global f, mL, m_, I, N
    majorana_state = (from_majorana_basis(to_majorana_basis(O, m_), mL) *
                      I).unit()
    if type(i) != type(None):
        return qt.expect(f[i].dag() * f[i], majorana_state)
    else:
        return qt.expect(N, majorana_state)
Beispiel #35
0
# Fidelity closer to 1 means the two states are similar to each other
H = snot(1, 0)
sigmax = sigmax()
sigmay = sigmay()
sigmaz = sigmaz()
SQRTNOT = sqrtnot(N=1, target=0)
T = Qobj([[1, 0], [0, cmath.rect(1, np.pi / 4)]])
S = Qobj([[1, 0], [0, 1j]])
target = 0
num_qubits = 1


# Tests for private functions
@pytest.mark.parametrize(
    "gate", [H, sigmax, sigmay, sigmaz, SQRTNOT, S, T,
             rand_unitary(2)])
@pytest.mark.parametrize("method",
                         [_ZYZ_rotation, _ZXZ_rotation, _ZYZ_pauli_X])
def test_single_qubit_to_rotations(gate, method):
    """Initial matrix and product of final decompositions are same within some
    phase."""
    gate_list = method(gate)
    circuit = QubitCircuit(num_qubits)
    circuit.add_gates(gate_list)
    decomposed_gates_final_matrix = circuit.compute_unitary()
    fidelity_of_input_output = average_gate_fidelity(
        gate, decomposed_gates_final_matrix)
    assert np.isclose(fidelity_of_input_output, 1.0)


@pytest.mark.parametrize(
Beispiel #36
0
    def testRandUnitary(self):
        "random Unitary"

        U = [rand_unitary(5) for k in range(5)]
        for u in U:
            assert_(u * u.dag() == qeye(5))
Beispiel #37
0
# # TO DO LIST:
# 
# ## - check the existing quantum architectures that we are using
# 
# ## - read on "proper" evolution with unitarity constraints
# 
# ## - code the circuit
# 
# ## - try a completely adaptive implementation
# 
# ## - consider a purely quantum optimisation

# In[34]:


A=qutip.rand_unitary(4).full()
A


# In[35]:


q,r=np.linalg.qr(A)


# In[36]:


np.imag(r)[abs(np.imag(r))<0.000001]=0
np.real(r)[abs(np.real(r))<0.000001]=0
r
Beispiel #38
0
    def testRandUnitary(self):
        "random Unitary"

        U = [rand_unitary(5) for k in range(5)]
        for u in U:
            assert_(u * u.dag() == qeye(5))