def generate_variable(name, hermitian=False, commutative=False): """Generates a commutative or noncommutative variable :param name: The symbolic name of the variable. :type name: str. :param hermitian: Optional parameter to request Hermitian variables . :type hermitian: bool. :param commutative: Optional parameter to request commutative variables. Commutative variables are Hermitian by default. :type commutative: bool. :returns: :class:`sympy.physics.quantum.operator.Operator` or :class:`sympy.physics.quantum.operator.HermitianOperator` variable :Example: >>> generate_variable("c", commutative=True) c """ if hermitian or commutative: variable = HermitianOperator(name) else: variable = Operator(name) variable.is_commutative = commutative return variable
def test_hermitian(): H = HermitianOperator('H') assert isinstance(H, HermitianOperator) assert isinstance(H, Operator) assert Dagger(H) == H assert H.inv() != H assert H.is_commutative is False assert Dagger(H).is_commutative is False
def non_commutative_sympify(expr_string, status, boolean): if "^" in expr_string: expr_string = expr_string.replace("^", "**") if "Ad" in expr_string: expr_string = expr_string.replace("Ad", "Dagger") fixed_string = "" if 'Dagger' in expr_string: fixed_string = expr_string.replace("Dagger", "sin") else: fixed_string = expr_string temp_evaluated_expr = parse_expr(fixed_string, evaluate=False) if status == '0': status1 = False elif status == '1': status1 = True new_locals = { sym.name: Symbol(sym.name, commutative=status1) for sym in temp_evaluated_expr.atoms(Symbol) } #new_locals = {} #for sym in temp_evaluated_expr.atoms(Symbol): # new_locals.update({sym.name:Operator(sym.name)}) #{'C': C, 'E': E, 'I': I, 'N': N, 'O': O, 'Q': Q, 'S': S} new_locals.update({'U': UnitaryOperator('U')}) new_locals.update({'c': Symbol('c', commutative=True)}) new_locals.update({'r': Symbol('r', commutative=True)}) new_locals.update({'t': Symbol('t', commutative=True)}) new_locals.update({'W': UnitaryOperator('W')}) new_locals.update({'V': UnitaryOperator('V')}) new_locals.update({'u': UnitaryOperator('u')}) new_locals.update({'w': UnitaryOperator('w')}) new_locals.update({'v': UnitaryOperator('v')}) new_locals.update({'H': HermitianOperator('H')}) new_locals.update({'A': HermitianOperator('A')}) new_locals.update({'T': HermitianOperator('T')}) new_locals.update({'C': Operator('C')}) new_locals.update({'Dagger': Dagger}) return sympify(expr_string, locals=new_locals, evaluate=boolean)
def generate_variables(n_vars, hermitian=False, commutative=False, name='x'): """Generates a number of commutative or noncommutative variables :param n_vars: The number of variables. :type n_vars: int. :returns: list of :class:`sympy.physics.quantum.operator.Operator` or :class:`sympy.physics.quantum.operator.HermitianOperator` variables """ variables = [] for i in range(n_vars): if hermitian or commutative: variables.append(HermitianOperator('%s%s' % (name, i))) else: variables.append(Operator('%s%s' % (name, i))) variables[i].is_commutative = commutative return variables
def test_sympy__physics__quantum__operator__HermitianOperator(): from sympy.physics.quantum.operator import HermitianOperator assert _test_args(HermitianOperator('H'))
# -*- coding: utf-8 -*- """ This script replicates the results of gloptipolydemo.m, which is packaged with Gloptipoly3. Created on Thu May 15 11:16:58 2014 @author: wittek """ from sympy.physics.quantum.operator import HermitianOperator from ncpol2sdpa import SdpRelaxation, write_to_sdpa # Get commutative variables x1 = HermitianOperator("x1") x1.is_commutative = True x2 = HermitianOperator("x2") x2.is_commutative = True g0 = 4 * x1 ** 2 + x1 * x2 - 4 * x2 ** 2 - \ 2.1 * x1 ** 4 + 4 * x2 ** 4 + x1 ** 6 / 3 # Obtain SDP relaxation sdpRelaxation = SdpRelaxation([x1, x2]) sdpRelaxation.get_relaxation(3, objective=g0) write_to_sdpa(sdpRelaxation, 'gloptipoly_demo.dat-s')