Make new Term from an Pauli operator
359022
def from_pauli(pauli, coeff=1.0):
        """"""
        if pauli.is_identity or coeff == 0:
            return Term((), coeff)
        return Term((pauli,), coeff)
582764
private static Operator substitute(Operator oper, List[] equatedIds) {
    Selector op1 = oper.getOperands()[0];
    Selector op2 = (oper.getOperands().length == 1) ? null : oper.getOperands()[1];
    if (op1 instanceof Identifier)
      op1 = substitute((Identifier) op1, equatedIds);
    else if (op1 instanceof Operator)
      op1 = substitute((Operator) op1, equatedIds);
    if (op1 == null)
      return null;
    if (op2 != null) {
      if (op2 instanceof Identifier)
        op2 = substitute((Identifier) op2, equatedIds);
      else if (op2 instanceof Operator)
        op2 = substitute((Operator) op2, equatedIds);
      if (op2 == null)
        return null;
    }
    if (op1 == oper.getOperands()[0] && (op2 == null || op2 == oper.getOperands()[1]))
      return oper;
    else if (oper instanceof LikeOperator) {
      LikeOperatorImpl loper = (LikeOperatorImpl) oper;
        return new LikeOperatorImpl(loper.getOp(), op1, loper.getInternalPattern(), loper.getPattern(), loper.isEscaped(), loper.getEscape());
    }
    else
      return (op2 == null) ? new OperatorImpl(oper.getOp(), op1) : new OperatorImpl(oper.getOp(), op1, op2);
  }
710568
private OperatorSymbol enterUnop(String name,
                                     Type arg,
                                     Type res,
                                     int opcode) {
        OperatorSymbol sym =
            new OperatorSymbol(makeOperatorName(name),
                               new MethodType(List.of(arg),
                                              res,
                                              List.<Type>nil(),
                                              methodClass),
                               opcode,
                               predefClass);
        predefClass.members().enter(sym);
        return sym;
    }
76587
def _one_body_mapping(a_i, a_j, threshold=0.000001):
        
        pauli_list = []
        for alpha in range(2):
            for beta in range(2):
                pauli_prod = Pauli.sgn_prod(a_i[alpha], a_j[beta])
                coeff = 1.0/4 * pauli_prod[1] * np.power(-1j, alpha) * np.power(1j, beta)
                pauli_term = [coeff, pauli_prod[0]]
                if np.absolute(pauli_term[0]) > threshold:
                    pauli_list.append(pauli_term)
        return Operator(paulis=pauli_list)
353488
def fromTerm(cls, term):
        """"""

        if isinstance(term, Term):
            term = term.handle
        elif not isinstance(term, (c_void_p, int)):
            raise ArgumentTypeError((str(Term), str(c_void_p)), str(type(term)))

        a = atom_t()
        if PL_get_atom(term, byref(a)):
            return cls(a.value)
676261
def inter_spin_hamiltonian(self, u_int, J_coup):
        """"""
        J_coup *= u_int
        h_int  = (u_int - 2*J_coup)/2.*self.oper['sumSz2']
        h_int += J_coup*self.oper['sumSz-sp2']
        h_int -= J_coup/2.*self.oper['sumSz-or2']
        h_int -= J_coup*self.oper['Sfliphop']

        return h_int
133466
def join_gates(*gates: Gate) -> Gate:
    """"""
    vectors = [gate.vec for gate in gates]
    vec = reduce(outer_product, vectors)
    return Gate(vec.tensor, vec.qubits)
295096
def expectation(self, operator: Union[PauliTerm, PauliSum]):
        
        if not isinstance(operator, PauliSum):
            operator = PauliSum([operator])

        return sum(_term_expectation(self.wf, term, n_qubits=self.n_qubits) for term in operator)
1314205
def from_list(cls, terms_list, coefficient=1.0):
        
        if not all([isinstance(op, tuple) for op in terms_list]):
            raise TypeError("The type of terms_list should be a list of (name, index) "
                            "tuples suitable for PauliTerm().")

        pterm = PauliTerm("I", 0)
        assert all([op[0] in PAULI_OPS for op in terms_list])

        indices = [op[1] for op in terms_list]
        assert all(_valid_qubit(index) for index in indices)

        # this is because from_list doesn't call simplify in order to be more efficient.
        if len(set(indices)) != len(indices):
            raise ValueError("Elements of PauliTerm that are allocated using from_list must "
                             "be on disjoint qubits. Use PauliTerm multiplication to simplify "
                             "terms instead.")

        for op, index in terms_list:
            if op != "I":
                pterm._ops[index] = op
        if not isinstance(coefficient, Number):
            raise ValueError("coefficient of PauliTerm must be a Number.")
        pterm.coefficient = complex(coefficient)
        return pterm
688569
def der(self, x: Sym):
        """"""
        name = 'der({:s})'.format(x.name())
        if name not in self.scope['dvar'].keys():
            self.scope['dvar'][name] = self.sym.sym(name, *x.shape)
            self.scope['states'].append(x.name())
        return self.scope['dvar'][name]
1070607
def remove_imaginary_terms(pauli_sums: PauliSum) -> PauliSum:
    
    if not isinstance(pauli_sums, PauliSum):
        raise TypeError("not a pauli sum. please give me one")
    new_term = sI(0) * 0.0
    for term in pauli_sums:
        new_term += term_with_coeff(term, term.coefficient.real)

    return new_term
1210390
def _bulk_to_linear(M, N, L, qubits):
    ""
    return [2 * L * N * x + 2 * L * y + L * u + k for x, y, u, k in qubits]
969537
def meff_SO(self, **kwargs):
        '''
        
        '''
        Eg = self.Eg_Gamma(**kwargs)
        Delta_SO = self.Delta_SO(**kwargs)
        Ep = self.Ep(**kwargs)
        luttinger1 = self.luttinger1(**kwargs)
        return 1./(luttinger1 - (Ep*Delta_SO)/(3*Eg*(Eg+Delta_SO)))
1144695
def operate(operator, other)
      exp = operator == '/' ? -1 : 1
      if other.respond_to?(:terms)
        Unit.new(other.terms.map { |t| t ** exp } << self)
      elsif other.respond_to?(:atom)
        Unit.new([self, other ** exp])
      elsif other.is_a?(Numeric)
        self.class.new(to_hash.merge(:factor => factor.send(operator, other)))
      end
    end
1050582
def operate(operator, other)
      exp = operator == '/' ? -1 : 1
      if other.respond_to?(:terms)
        self.class.new(terms + other.terms.map { |t| t ** exp })
      elsif other.respond_to?(:atom)
        self.class.new(terms << other ** exp)
      elsif other.is_a?(Numeric)
        self.class.new(terms.map { |t| t.send(operator, other) })
      end
    end
633800
def conjugate(self):
        """"""
        return self.__class__.create(self.term.conjugate(), *self.ranges)
155889
def Incr(self, x, term=1):
        
        self.d[x] = self.d.get(x, 0) + term