Exemplo n.º 1
0
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)
Exemplo n.º 2
0
 def __init__(self,
              paulistring: PauliString,
              angle: float,
              control: typing.List[int] = None):
     super().__init__(name="Exp-Pauli",
                      target=tuple(t for t in paulistring.keys()),
                      control=control,
                      parameter=angle)
     self.paulistring = paulistring
     self.finalize()
Exemplo n.º 3
0
 def to_pauli_strings(self):
     data = {}
     for i in range(self.n_qubit):
         if self.has_x(i):
             data[i] = 'X'
         elif self.has_y(i):
             data[i] = 'Y'
         elif self.has_z(i):
             data[i] = 'Z'
     return PauliString(data, self.coeff)
Exemplo n.º 4
0
 def __init__(self,
              paulistring: PauliString,
              angle: float,
              control: typing.List[int] = None):
     super().__init__(eigenvalues_magnitude=0.5,
                      name="Exp-Pauli",
                      target=tuple(t for t in paulistring.keys()),
                      control=control,
                      parameter=angle)
     self.paulistring = paulistring
     self.generator = QubitHamiltonian.from_paulistrings(paulistring)
     self.finalize()
Exemplo n.º 5
0
def ExpPauli(paulistring: typing.Union[PauliString, str],
             angle,
             control: typing.Union[list, int] = None):
    """Exponentiated Pauligate:
    
    ExpPauli(PauliString, angle) = exp(-i* angle/2* PauliString)

    Parameters
    ----------
    paulistring :
        given as PauliString structure or as string or dict or list
        if given as string: Format should be like X(0)Y(3)Z(2)
        if given as list: Format should be like [(0,'X'),(3,'Y'),(2,'Z')]
        if given as dict: Format should be like { 0:'X', 3:'Y', 2:'Z' }
    angle :
        the angle (will be multiplied by paulistring coefficient if there is one)
    control :
        control qubits
    paulistring: typing.Union[PauliString :
        
    str] :
        
    control: typing.Union[list :
        
    int] :
         (Default value = None)

    Returns
    -------
    type
        Gate wrapped in circuit

    """

    if isinstance(paulistring, str):
        ps = PauliString.from_string(string=paulistring)
    elif isinstance(paulistring, list):
        ps = PauliString.from_openfermion(key=list)
    elif isinstance(paulistring, dict):
        ps = PauliString(data=paulistring)
    else:
        ps = paulistring

    # Failsave: If the paulistring contains just one pauli matrix
    # it is better to initialize a rotational gate due to strange conventions in some simulators
    if len(ps.items()) == 1:
        target, axis = tuple(ps.items())[0]
        return QCircuit.wrap_gate(
            impl.RotationGateImpl(axis=axis,
                                  target=target,
                                  angle=ps.coeff * assign_variable(angle),
                                  control=control))
    else:
        return QCircuit.wrap_gate(
            impl.ExponentialPauliGateImpl(paulistring=ps,
                                          angle=angle,
                                          control=control))
Exemplo n.º 6
0
from tequila.hamiltonian import PauliString

if __name__ == "__main__":
    testdata = {0: 'x', 1: 'y', 2: 'z'}
    test = PauliString(data=testdata, coeff=2)
    print("test=", test)
    print("test: openfermion_key = ", test.key_openfermion())
    print(
        "reinitialized: ",
        PauliString.from_openfermion(key=test.key_openfermion(),
                                     coeff=test.coeff))