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)
Exemple #2
0
def test_initialization():
    H = paulis.I()
    for i in range(10):
        H += paulis.pauli(qubit=numpy.random.randint(0,5,3), type=numpy.random.choice(["X", "Y", "Z"],1))

    for H1 in [H, paulis.I(), paulis.Zero(), paulis.X(0), paulis.Y(1), 1.234*paulis.Z(2)]:
        string = str(H1)
        ofstring = str(H1.to_openfermion())
        H2 = QubitHamiltonian.from_string(string=string)
        assert H1 == H2
        H3 = QubitHamiltonian.from_string(string=ofstring, openfermion_format=True)
        assert H1 == H3
def test_paulistring_conversion():
    X1 = QubitHamiltonian.from_string("X0", openfermion_format=True)
    X2 = paulis.X(0)
    keys = [i for i in X2.keys()]
    pwx = PauliString.from_openfermion(key=keys[0], coeff=X2[keys[0]])
    X3 = QubitHamiltonian.from_paulistrings(pwx)
    assert (X1 == X2)
    assert (X2 == X3)

    H = paulis.X(0) * paulis.Y(1) * paulis.Z(2) + paulis.X(3) * paulis.Y(
        4) * paulis.Z(5)
    PS = []
    for key, value in H.items():
        PS.append(PauliString.from_openfermion(key, value))
    PS2 = H.paulistrings
    assert (PS == PS2)

    H = make_random_pauliword(complex=True)
    for i in range(5):
        H += make_random_pauliword(complex=True)
    PS = []
    for key, value in H.items():
        PS.append(PauliString.from_openfermion(key, value))
    PS2 = H.paulistrings
    assert (PS == PS2)
Exemple #4
0
def pauli(qubit, type) -> QubitHamiltonian:
    """
    Parameters
    ----------
    qubit: int or list of ints

    type: str or int or list of string or int:
        define if X, Y or Z (0,1,2)

    Returns
    -------
    QubitHamiltonian
    """
    def assign_axis(axis):
        if axis in QubitHamiltonian.axis_to_string:
            return QubitHamiltonian.axis_to_string[axis]
        elif hasattr(axis, "upper"):
            return axis.upper()
        else:
            raise TequilaException(
                "unknown initialization for pauli operator: {}".format(axis))

    if not isinstance(qubit, typing.Iterable):
        qubit = [qubit]
        type = [type]

    type = [assign_axis(x) for x in type]

    init_string = "".join("{}{} ".format(t, q) for t, q in zip(type, qubit))

    return QubitHamiltonian.from_string(string=init_string,
                                        openfermion_format=True)
def test_convenience():

    i = numpy.random.randint(0, 10, 1)[0]
    assert paulis.X(i) + paulis.I(i) == paulis.X(i) + 1.0

    assert paulis.Qp(i) == 0.5 * (1.0 + paulis.Z(i))
    assert paulis.Qm(i) == 0.5 * (1.0 - paulis.Z(i))
    assert paulis.Sp(i) == 0.5 * (paulis.X(i) + 1.j * paulis.Y(i))
    assert paulis.Sm(i) == 0.5 * (paulis.X(i) - 1.j * paulis.Y(i))

    i = numpy.random.randint(0, 10, 1)[0]
    assert paulis.Qp(i) == (0.5 + 0.5 * paulis.Z(i))
    assert paulis.Qm(i) == (0.5 - 0.5 * paulis.Z(i))
    assert paulis.Sp(i) == (0.5 * paulis.X(i) + 0.5j * paulis.Y(i))
    assert paulis.Sm(i) == (0.5 * paulis.X(i) - 0.5j * paulis.Y(i))

    assert -1.0 * paulis.Y(i) == -paulis.Y(i)

    test = paulis.Z(i)
    test *= -1.0
    assert test == -paulis.Z(i)

    test = paulis.Z(i)
    test += 1.0
    assert test == paulis.Z(i) + 1.0

    test = paulis.X(i)
    test += paulis.Y(i + 1)
    assert test == paulis.X(i) + paulis.Y(i + 1)

    test = paulis.X(i)
    test -= paulis.Y(i)
    test += 3.0
    test = -test
    assert test == -1.0 * (paulis.X(i) - paulis.Y(i) + 3.0)

    test = paulis.X([0, 1, 2, 3])
    assert test == QubitHamiltonian.from_string("X(0)X(1)X(2)X(3)", False)

    test = paulis.Y([0, 1, 2, 3])
    assert test == QubitHamiltonian.from_string("Y(0)Y(1)Y(2)Y(3)", False)

    test = paulis.Z([0, 1, 2, 3])
    assert test == QubitHamiltonian.from_string("Z(0)Z(1)Z(2)Z(3)", False)
Exemple #6
0
 def __init__(self,
              name,
              generator: QubitHamiltonian,
              target: list,
              power,
              control: list = None):
     if generator is None:
         assert name is not None and name.upper() in ["X", "Y", "Z"]
         generator = QubitHamiltonian.from_string("{}({})".format(
             name.upper(), target))
     if name is None:
         assert generator is not None
         name = str(generator)
     super().__init__(name=name,
                      parameter=power * pi,
                      target=target,
                      control=control,
                      generator=generator)
Exemple #7
0
def test_simple_trace_out():
    H1 = QubitHamiltonian.from_string("1.0*Z(0)*Z(1)")
    H2 = QubitHamiltonian.from_string("1.0*Z(0)")
    assert H2 == H1.trace_out_qubits(qubits=[1], states=None)

    H1 = QubitHamiltonian.from_string("1.0*Z(0)*Z(1)X(100)")
    H2 = QubitHamiltonian.from_string("1.0*Z(1)X(100)")
    assert H2 == H1.trace_out_qubits(qubits=[0], states=None)

    H1 = QubitHamiltonian.from_string("1.0*Z(0)*Z(1)X(100)")
    H2 = QubitHamiltonian.from_string("-1.0*Z(0)X(100)")
    assert H2 == H1.trace_out_qubits(qubits=[1], states=[QubitWaveFunction.from_string("1.0*|1>")])

    H1 = QubitHamiltonian.from_string("1.0*Z(0)*Z(1)X(100)")
    H2 = QubitHamiltonian.from_string("-1.0*Z(1)X(100)")
    assert H2 == H1.trace_out_qubits(qubits=[0], states=[QubitWaveFunction.from_string("1.0*|1>")])

    H1 = QubitHamiltonian.from_string("1.0*X(0)*Z(1)*Z(5)*X(100)Y(50)")
    H2 = QubitHamiltonian.from_string("1.0*X(0)X(100)Y(50)")
    assert H2 == H1.trace_out_qubits(qubits=[1,5], states=[QubitWaveFunction.from_string("1.0*|1>")]*2)

    H1 = QubitHamiltonian.from_string("1.0*X(0)*Z(1)*X(100)Y(50)")
    H2 = QubitHamiltonian.from_string("-1.0*X(0)X(100)Y(50)")
    assert H2 == H1.trace_out_qubits(qubits=[1,5], states=[QubitWaveFunction.from_string("1.0*|1>")]*2)