Exemple #1
0
def test_trace_out_xy(theta):
    a = numpy.sin(theta)
    b = numpy.cos(theta)
    state = QubitWaveFunction.from_array([a,b])

    H1 = QubitHamiltonian.from_string("1.0*X(0)*X(1)*X(100)")
    H2 = QubitHamiltonian.from_string("1.0*X(0)*X(100)")
    factor = a.conjugate()*b + b.conjugate()*a
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)
    factor *= factor
    H1 = QubitHamiltonian.from_string("1.0*X(0)*X(1)*X(5)*X(100)")
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)


    H1 = QubitHamiltonian.from_string("1.0*X(0)*Y(1)*X(100)")
    H2 = QubitHamiltonian.from_string("1.0*X(0)*X(100)")
    factor = -1.0j*(a.conjugate()*b - b.conjugate()*a)
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)
    factor *= factor
    H1 = QubitHamiltonian.from_string("1.0*X(0)*Y(1)*Y(5)*X(100)")
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)

    H1 = QubitHamiltonian.from_string("1.0*X(0)*X(1)*X(100)")
    H2 = QubitHamiltonian.from_string("1.0*X(0)*X(100)")
    factor = a.conjugate()*b + b.conjugate()*a
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)
    factor *= -1.0j*(a.conjugate()*b - b.conjugate()*a)
    H1 = QubitHamiltonian.from_string("1.0*X(0)*X(1)*Y(5)*X(100)")
    assert factor*H2 == H1.trace_out_qubits(qubits=[1,3,5], states=[state]*3)
def test_projectors(qubits):
    real = numpy.random.uniform(0.0, 1.0, 2**qubits)
    imag = numpy.random.uniform(0.0, 1.0, 2**qubits)
    array = real + 1.j * imag
    wfn = QubitWaveFunction.from_array(arr=array)
    P = paulis.Projector(wfn=wfn.normalize())
    assert (P.is_hermitian())
    assert (wfn.apply_qubitoperator(P) == wfn)
    PM = P.to_matrix()
    assert ((PM.dot(PM) == PM).all)
def test_ketbra_random(n_qubits):
    ket = numpy.random.uniform(0.0, 1.0, 2**n_qubits)
    bra = QubitWaveFunction.from_int(0, n_qubits=n_qubits)
    operator = paulis.KetBra(ket=ket, bra=bra)
    result = operator * bra
    assert result == QubitWaveFunction.from_array(ket)