K iJ%ddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZmZddlmZGddeZdZ dZ!y))product)Add)Tuple)expand)Mul)Slog)MutableDenseMatrix prettyForm)Dagger)HermitianOperator) represent) numpy_ndarrayscipy_sparse_matrixto_numpy)TrcteZdZdZefdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZxZS)Densitya Density operator for representing mixed states. TODO: Density operator support for Qubits Parameters ========== values : tuples/lists Each tuple/list should be of form (state, prob) or [state,prob] Examples ======== Create a density operator with 2 states represented by Kets. >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d Density((|0>, 0.5),(|1>, 0.5)) ct||}|D]+}t|trt |dk(r"t d|S)Nz?Each argument should be of form [state,prob] or ( state, prob ))super _eval_args isinstancerlen ValueError)clsargsarg __class__s c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/density.pyrzDensity._eval_args)sPw!$' 8CsE*s3x1} "788 8  cRt|jDcgc]}|d c}Scc}w)aReturn list of all states. Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.states() (|0>, |1>) rrrselfr s r"stateszDensity.states6%3#s1v3443 $cRt|jDcgc]}|d c}Scc}w)a#Return list of all probabilities. Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.probs() (0.5, 0.5) r%r&s r"probsz Density.probsEr)r*c*|j|d}|S)atReturn specific state by index. Parameters ========== index : index of state to be returned Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.states()[1] |1> rr)r'indexstates r" get_statezDensity.get_stateTs$ % # r#c*|j|d}|S)aReturn probability of specific state by index. Parameters =========== index : index of states whose probability is returned. Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.probs()[1] 0.500000000000000 r,r/)r'r0probs r"get_probzDensity.get_probis$yy" r#cd|jDcgc] \}}||z|f}}}t|Scc}}w)aop will operate on each individual state. Parameters ========== op : Operator Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> from sympy.physics.quantum.operator import Operator >>> A = Operator('A') >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.apply_op(A) Density((A*|0>, 0.5),(A*|1>, 0.5)) )rr)r'opr1r4new_argss r"apply_opzDensity.apply_op~s9(;?))D%RXt$DD!!Es,c Tg}|jD]\}}|j}t|trGt |jdD],}|j ||j |d|dz.m|j ||j ||zt|S)aExpand the density operator into an outer product format. Examples ======== >>> from sympy.physics.quantum.state import Ket >>> from sympy.physics.quantum.density import Density >>> from sympy.physics.quantum.operator import Operator >>> A = Operator('A') >>> d = Density([Ket(0), 0.5], [Ket(1),0.5]) >>> d.doit() 0.5*|0><0| + 0.5*|1><1| r)repeatrr,)rrrrrappend_generate_outer_prod)r'hintstermsr1r4r s r"doitz Density.doits !YY KMUDLLNE5#&"5::a8ICLLd&?&?A@CA'H"HII T$";";E5"IIJ KE{r#c|j\}}|j\}}t|dk(st|dk(r tdt|t t|z}t|t|z|zS)NrzHAtleast one-pair of Non-commutative instance required for outer product.)args_cncrrrr)r'arg1arg2c_part1nc_part1c_part2nc_part2r7s r"r=zDensity._generate_outer_prods| MMO MMO MQ #h-1"434 4(^F3>2 2G}S']*R//r#c 6t|jfi|SN)rr@)r'optionss r" _representzDensity._represents000r#cy)Nz\rhor'printerrs r"_print_operator_name_latexz"Density._print_operator_name_latexsr#ctdS)Nuρr rOs r"_print_operator_name_prettyz#Density._print_operator_name_prettys677r#c v|jdg}t|j|jS)Nindices)getrr@)r'kwargsrUs r" _eval_tracezDensity._eval_traces.**Y+$))+w',,..r#ct|S)zl Compute the entropy of a density matrix. Refer to density.entropy() method for examples. )entropy)r's r"rZzDensity.entropys t}r#)__name__ __module__ __qualname____doc__ classmethodrr(r-r2r5r9r@r=rLrQrSrXrZ __classcell__)r!s@r"rrsX,   5 5**".80 18/r#rct|tr t|}t|tr t |}t|t r:|j j}ttd|D St|trCddl }|jj|}|j||j|z Std)aCompute the entropy of a matrix/density object. This computes -Tr(density*ln(density)) using the eigenvalue decomposition of density, which is given as either a Density instance or a matrix (numpy.ndarray, sympy.Matrix or scipy.sparse). Parameters ========== density : density matrix of type Density, SymPy matrix, scipy.sparse or numpy.ndarray Examples ======== >>> from sympy.physics.quantum.density import Density, entropy >>> from sympy.physics.quantum.spin import JzKet >>> from sympy import S >>> up = JzKet(S(1)/2,S(1)/2) >>> down = JzKet(S(1)/2,-S(1)/2) >>> d = Density((up,S(1)/2),(down,S(1)/2)) >>> entropy(d) log(2)/2 c38K|]}|t|zywrJr ).0es r" zentropy..s51SV85srNz4numpy.ndarray, scipy.sparse or SymPy matrix expected)rrrrrMatrix eigenvalskeysrsumrnumpylinalgeigvalsr r)densityrlnps r"rZrZs4'7#G$'./7#'6"##%**,s5W55566 G] +))##G,wrvvg./// BD Dr#ct|tr t|n|}t|tr t|n|}t|trt|ts$t dt |dt |d|j |j k7r|jr t d|tjz}t||z|ztjzjS)a Computes the fidelity [1]_ between two quantum states The arguments provided to this function should be a square matrix or a Density object. If it is a square matrix, it is assumed to be diagonalizable. Parameters ========== state1, state2 : a density matrix or Matrix Examples ======== >>> from sympy import S, sqrt >>> from sympy.physics.quantum.dagger import Dagger >>> from sympy.physics.quantum.spin import JzKet >>> from sympy.physics.quantum.density import fidelity >>> from sympy.physics.quantum.represent import represent >>> >>> up = JzKet(S(1)/2,S(1)/2) >>> down = JzKet(S(1)/2,-S(1)/2) >>> amp = 1/sqrt(2) >>> updown = (amp*up) + (amp*down) >>> >>> # represent turns Kets into matrices >>> up_dm = represent(up*Dagger(up)) >>> down_dm = represent(down*Dagger(down)) >>> updown_dm = represent(updown*Dagger(updown)) >>> >>> fidelity(up_dm, up_dm) 1 >>> fidelity(up_dm, down_dm) #orthogonal states 0 >>> fidelity(up_dm, updown_dm).evalf().round(3) 0.707 References ========== .. [1] https://en.wikipedia.org/wiki/Fidelity_of_quantum_states zBstate1 and state2 must be of type Density or Matrix received type=z for state1 and type=z for state2z]The dimensions of both args should be equal and the matrix obtained should be a square matrix) rrrrfrtypeshape is_squarerHalfrr@)state1state2 sqrt_state1s r"fidelityrwsX#-VW"=Yv 6F",VW"=Yv 6F ff %Z-Gv,V 67 7||v||#(8(8EF F!&&.K {6!+-6 7 < < >>r#N)" itertoolsrsympy.core.addrsympy.core.containersrsympy.core.functionrsympy.core.mulrsympy.core.singletonr&sympy.functions.elementary.exponentialr sympy.matrices.denser rf sympy.printing.pretty.stringpictr sympy.physics.quantum.daggerrsympy.physics.quantum.operatorrsympy.physics.quantum.representr!sympy.physics.quantum.matrixutilsrrrsympy.physics.quantum.tracerrrZrwrNr#r"rsN'&"6=7/<5ZZ*AAH)DX9?r#