K ik6ddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z ddl mZddlmZdd lmZdd lmZgd Zed Zed ddgiZd!dZd"dZerereZneZdZdZdZdZdZd#dZ d#dZ!GddeZ"dZ#dZ$ d$dZ%d Z&y)%)deque)randint) import_module)BasicMul)Number equal_valued)Pow)S) represent)Dagger) generate_gate_rulesgenerate_equivalent_ids GateIdentitybfs_identity_searchrandom_identity_searchis_scalar_sparse_matrixis_scalar_nonsparse_matrix is_degenerate is_reduciblenumpyscipyfromlistsparse) import_kwargsctrts tt||d}t |t r |r|dk(SdS|j j}tj|j| kD|j|k}tj|j| kD|j|k}tj|d|j}tj|d|j} | tdz} || z} | jd} | jd} | | k(} | j}| dd}|dk(ry | |z j}t!d |}||k(}t#|jd z |k}t#|j|k}|xr|}|r|nd}t%|xr|xr|S) aChecks if a given scipy.sparse matrix is a scalar matrix. A scalar matrix is such that B = bI, where B is the scalar matrix, b is some scalar multiple, and I is the identity matrix. A scalar matrix would have only the element b along it's main diagonal and zeroes elsewhere. Parameters ========== circuit : Gate tuple Sequence of quantum gates representing a quantum circuit nqubits : int Number of qubits in the circuit identity_only : bool Check for only identity matrices eps : number The tolerance value for zeroing out elements in the matrix. Values in the range [-eps, +eps] will be changed to a zero. z scipy.sparse)nqubitsformatTgy?ryFg?)nprr r isinstanceinttodensegetA logical_andrealimagwherecomplexnonzeroalltracepowabsbool)circuitr identity_onlyepsmatrix dense_matrix bool_real bool_imagcorrected_realcorrected_imagcorrected_dense row_indices col_indices bool_indices is_diagonal first_elementtrace_of_correctedexpected_tracehas_correct_trace real_is_one imag_is_zerois_one is_identitys j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/quantum/identitysearch.pyrr s, U sG}g,.F 63+v{55~~',,. NN<#4#4t#;#/#4#4s#:< NN<#4#4t#;#/#4#4s#:< )S,2C2CD)S,2C2CD''"+5(>9&--/2 %--/2 #k1 "&&( '*1-  Z ' .m;BBDQ..@-,,s23c9 =--.4 - -f4 KE$5E+FFNctt||}t|tr |r|dk(SdS|j }|s||dz n|}|rt |ddnd}|t d|k(}t|jxr|xr|S)aChecks if a given circuit, in matrix form, is equivalent to a scalar value. Parameters ========== circuit : Gate tuple Sequence of quantum gates representing a quantum circuit nqubits : int Number of qubits in the circuit identity_only : bool Check for only identity matrices eps : number This argument is ignored. It is just for signature compatibility with is_scalar_sparse_matrix. Note: Used in situations when is_scalar_sparse_matrix has bugs )rr Trr!) r rr#r r.r r/r1r?) r2rr3r4r5 matrix_traceadjusted_matrix_tracerGrCs rHrrxs(sG}g 6F 66"+v{55 ||~ )6".fQi!7&2 5Bl6!9a0t 1SG_D    F%6 F;H HrIcft|tr|jjS|jSN)r#r base min_qubits)a_gates rH_get_min_qubitsrRs(&#{{%%%   rIct|dkDr&|d}tt||ft|d}t|dkDr#r!|dt|}tf|z}||fSy)aPerform a LL operation. A LL operation multiplies both left and right circuits with the dagger of the left circuit's leftmost gate, and the dagger is multiplied on the left side of both circuits. If a LL is possible, it returns the new gate rule as a 2-tuple (LHS, RHS), where LHS is the left circuit and and RHS is the right circuit of the new rule. If a LL is not possible, None is returned. Parameters ========== left : Gate tuple The left circuit of a gate rule expression. right : Gate tuple The right circuit of a gate rule expression. Examples ======== Generate a new gate rule using a LL operation: >>> from sympy.physics.quantum.identitysearch import ll_op >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> ll_op((x, y, z), ()) ((Y(0), Z(0)), (X(0),)) >>> ll_op((y, z), (x,)) ((Z(0),), (Y(0), X(0))) rTr Nlenis_scalar_matrixrrR)leftrightll_gatell_gate_is_unitarynew_left new_rights rHll_opr]syF D A q'- G_g &(@$H D A ,#d)$G_&. )$$ rIct|dkDr2|t|dz }tt||ft|d}t|dkDr&r$|dt|dz }|tfz}||fSy)aPerform a LR operation. A LR operation multiplies both left and right circuits with the dagger of the left circuit's rightmost gate, and the dagger is multiplied on the right side of both circuits. If a LR is possible, it returns the new gate rule as a 2-tuple (LHS, RHS), where LHS is the left circuit and and RHS is the right circuit of the new rule. If a LR is not possible, None is returned. Parameters ========== left : Gate tuple The left circuit of a gate rule expression. right : Gate tuple The right circuit of a gate rule expression. Examples ======== Generate a new gate rule using a LR operation: >>> from sympy.physics.quantum.identitysearch import lr_op >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> lr_op((x, y, z), ()) ((X(0), Y(0)), (Z(0),)) >>> lr_op((x, y), (z,)) ((X(0),), (Z(0), Y(0))) rr TNrT)rWrXlr_gatelr_gate_is_unitaryr[r\s rHlr_oprasF D A s4y1}%- G_g &(@$H D A ,#d)a-(VG_.. )$$ rIct|dkDr&|d}tt||ft|d}t|dkDr#r!|dt|}tf|z}||fSy)aPerform a RL operation. A RL operation multiplies both left and right circuits with the dagger of the right circuit's leftmost gate, and the dagger is multiplied on the left side of both circuits. If a RL is possible, it returns the new gate rule as a 2-tuple (LHS, RHS), where LHS is the left circuit and and RHS is the right circuit of the new rule. If a RL is not possible, None is returned. Parameters ========== left : Gate tuple The left circuit of a gate rule expression. right : Gate tuple The right circuit of a gate rule expression. Examples ======== Generate a new gate rule using a RL operation: >>> from sympy.physics.quantum.identitysearch import rl_op >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> rl_op((x,), (y, z)) ((Y(0), X(0)), (Z(0),)) >>> rl_op((x, y), (z,)) ((Z(0), X(0), Y(0)), ()) rTr NrT)rWrXrl_gaterl_gate_is_unitaryr\r[s rHrl_opresyF E Q(- G_g &(@$H E Q-!CJ' 7O%,)$$ rIct|dkDr2|t|dz }tt||ft|d}t|dkDr&r$|dt|dz }|tfz}||fSy)aPerform a RR operation. A RR operation multiplies both left and right circuits with the dagger of the right circuit's rightmost gate, and the dagger is multiplied on the right side of both circuits. If a RR is possible, it returns the new gate rule as a 2-tuple (LHS, RHS), where LHS is the left circuit and and RHS is the right circuit of the new rule. If a RR is not possible, None is returned. Parameters ========== left : Gate tuple The left circuit of a gate rule expression. right : Gate tuple The right circuit of a gate rule expression. Examples ======== Generate a new gate rule using a RR operation: >>> from sympy.physics.quantum.identitysearch import rr_op >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> rr_op((x, y), (z,)) ((X(0), Y(0), Z(0)), ()) >>> rr_op((x,), (y, z)) ((X(0), Z(0)), (Y(0),)) rr TNrT)rWrXrr_gaterr_gate_is_unitaryr\r[s rHrr_opriMsF E QE Q'- G_g &(@$H E Q-!CJN+ 6'?,,)$$ rIc t|tr&|r!tjtjfhSdhSt|tr |j }t t t| fd} j|ddf j|dft dkDrw j\}}}t||}|||t||}|||t||}|||t||}|||t dkDrw|r7t} D]&}|\}}|jt |t |f(| S)am Returns a set of gate rules. Each gate rules is represented as a 2-tuple of tuples or Muls. An empty tuple represents an arbitrary scalar value. This function uses the four operations (LL, LR, RL, RR) to generate the gate rules. A gate rule is an expression such as ABC = D or AB = CD, where A, B, C, and D are gates. Each value on either side of the equal sign represents a circuit. The four operations allow one to find a set of equivalent circuits from a gate identity. The letters denoting the operation tell the user what activities to perform on each expression. The first letter indicates which side of the equal sign to focus on. The second letter indicates which gate to focus on given the side. Once this information is determined, the inverse of the gate is multiplied on both circuits to create a new gate rule. For example, given the identity, ABCD = 1, a LL operation means look at the left value and multiply both left sides by the inverse of the leftmost gate A. If A is Hermitian, the inverse of A is still A. The resulting new rule is BCD = A. The following is a summary of the four operations. Assume that in the examples, all gates are Hermitian. LL : left circuit, left multiply ABCD = E -> AABCD = AE -> BCD = AE LR : left circuit, right multiply ABCD = E -> ABCDD = ED -> ABC = ED RL : right circuit, left multiply ABC = ED -> EABC = EED -> EABC = D RR : right circuit, right multiply AB = CD -> ABD = CDD -> ABD = C The number of gate rules generated is n*(n+1), where n is the number of gates in the sequence (unproven). Parameters ========== gate_seq : Gate tuple, Mul, or Number A variable length tuple or Mul of Gates whose product is equal to a scalar matrix return_as_muls : bool True to return a set of Muls; False to return a set of tuples Examples ======== Find the gate rules of the current circuit using tuples: >>> from sympy.physics.quantum.identitysearch import generate_gate_rules >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> generate_gate_rules((x, x)) {((X(0),), (X(0),)), ((X(0), X(0)), ())} >>> generate_gate_rules((x, y, z)) {((), (X(0), Z(0), Y(0))), ((), (Y(0), X(0), Z(0))), ((), (Z(0), Y(0), X(0))), ((X(0),), (Z(0), Y(0))), ((Y(0),), (X(0), Z(0))), ((Z(0),), (Y(0), X(0))), ((X(0), Y(0)), (Z(0),)), ((Y(0), Z(0)), (X(0),)), ((Z(0), X(0)), (Y(0),)), ((X(0), Y(0), Z(0)), ()), ((Y(0), Z(0), X(0)), ()), ((Z(0), X(0), Y(0)), ())} Find the gate rules of the current circuit using Muls: >>> generate_gate_rules(x*x, return_as_muls=True) {(1, 1)} >>> generate_gate_rules(x*y*z, return_as_muls=True) {(1, X(0)*Z(0)*Y(0)), (1, Y(0)*X(0)*Z(0)), (1, Z(0)*Y(0)*X(0)), (X(0)*Y(0), Z(0)), (Y(0)*Z(0), X(0)), (Z(0)*X(0), Y(0)), (X(0)*Y(0)*Z(0), 1), (Y(0)*Z(0)*X(0), 1), (Z(0)*X(0)*Y(0), 1), (X(0), Z(0)*Y(0)), (Y(0), X(0)*Z(0)), (Z(0), Y(0)*X(0))} )rkc|B|\}}|vr||fvrj||dzkrj||dzfzyyy)Nr )addappend)new_ruleopsr[r\max_opsqueueruless rHprocess_new_rulez-generate_gate_rules..process_new_rules`  "* Hiu$)X)>e)K (#Qw  Xq 23! rIrkr)r#r r OnerargsrsetrUrnrmpopleftr]rareri) gate_seqreturn_as_mulsrtrWrXrpro mul_rulesrulerqrrrss @@@rHrrsSd(F# UUAEEN# #:  Hc "== GE EE(mG4 LL(B"# IIxn e*q. ==?eSu%3'u%3'u%3'u%3' e*q. E  5DKD% MM3:sE{3 4 5 LrIcRt|trtjhSt|tr |j }t }t|}|D]5}|\}}|dk(r|j||dk(s%|j|7|rd}t t||}|S)aReturns a set of equivalent gate identities. A gate identity is a quantum circuit such that the product of the gates in the circuit is equal to a scalar value. For example, XYZ = i, where X, Y, Z are the Pauli gates and i is the imaginary value, is considered a gate identity. This function uses the four operations (LL, LR, RL, RR) to generate the gate rules and, subsequently, to locate equivalent gate identities. Note that all equivalent identities are reachable in n operations from the starting gate identity, where n is the number of gates in the sequence. The max number of gate identities is 2n, where n is the number of gates in the sequence (unproven). Parameters ========== gate_seq : Gate tuple, Mul, or Number A variable length tuple or Mul of Gates whose product is equal to a scalar matrix. return_as_muls: bool True to return as Muls; False to return as tuples Examples ======== Find equivalent gate identities from the current circuit with tuples: >>> from sympy.physics.quantum.identitysearch import generate_equivalent_ids >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> generate_equivalent_ids((x, x)) {(X(0), X(0))} >>> generate_equivalent_ids((x, y, z)) {(X(0), Y(0), Z(0)), (X(0), Z(0), Y(0)), (Y(0), X(0), Z(0)), (Y(0), Z(0), X(0)), (Z(0), X(0), Y(0)), (Z(0), Y(0), X(0))} Find equivalent gate identities from the current circuit with Muls: >>> generate_equivalent_ids(x*x, return_as_muls=True) {1} >>> generate_equivalent_ids(x*y*z, return_as_muls=True) {X(0)*Y(0)*Z(0), X(0)*Z(0)*Y(0), Y(0)*X(0)*Z(0), Y(0)*Z(0)*X(0), Z(0)*X(0)*Y(0), Z(0)*Y(0)*X(0)} rkct|SrNr)id_seqs rHz)generate_equivalent_ids..Zs V rI) r#r r rurrvrwrrmmap)ryrzeq_ids gate_rulesr|lrconvert_to_muls rHrrsj(F#w Hc "== UF$X.J1 7 JJqM "W JJqM 4S01 MrIc\eZdZdZdZedZedZedZedZ dZ y) raWrapper class for circuits that reduce to a scalar value. A gate identity is a quantum circuit such that the product of the gates in the circuit is equal to a scalar value. For example, XYZ = i, where X, Y, Z are the Pauli gates and i is the imaginary value, is considered a gate identity. Parameters ========== args : Gate tuple A variable length tuple of Gates that form an identity. Examples ======== Create a GateIdentity and look at its attributes: >>> from sympy.physics.quantum.identitysearch import GateIdentity >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> an_identity = GateIdentity(x, y, z) >>> an_identity.circuit X(0)*Y(0)*Z(0) >>> an_identity.equivalent_ids {(X(0), Y(0), Z(0)), (X(0), Z(0), Y(0)), (Y(0), X(0), Z(0)), (Y(0), Z(0), X(0)), (Z(0), X(0), Y(0)), (Z(0), Y(0), X(0))} ctj|g|}t||_t ||_t ||_|SrN)r__new__r_circuitr_rulesr_eq_ids)clsrvobjs rHrzGateIdentity.__new__s=mmC'$'Dz (. -d3  rIc|jSrN)rselfs rHr2zGateIdentity.circuits }}rIc|jSrN)rrs rHrzGateIdentity.gate_ruless {{rIc|jSrN)rrs rHequivalent_idszGateIdentity.equivalent_idss ||rIc|jSrN)rvrs rHsequencezGateIdentity.sequences yyrIc,t|jS)z'Returns the string of gates in a tuple.)strr2rs rH__str__zGateIdentity.__str__s4<<  rIN) __name__ __module__ __qualname____doc__rpropertyr2rrrrrkrIrHrr`sa<!rIrc2|D]}||jvsyy)aChecks if a gate identity is a permutation of another identity. Parameters ========== identity_set : set A Python set with GateIdentity objects. gate_identity : GateIdentity The GateIdentity to check for existence in the set. Examples ======== Check if the identity is a permutation of another identity: >>> from sympy.physics.quantum.identitysearch import ( ... GateIdentity, is_degenerate) >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> an_identity = GateIdentity(x, y, z) >>> id_set = {an_identity} >>> another_id = (y, z, x) >>> is_degenerate(id_set, another_id) True >>> another_id = (x, x) >>> is_degenerate(id_set, another_id) False TF)r) identity_set gate_identityan_ids rHrrs*B U11 1 rIcpd}tt||D]}||}|f|z}t||dsyy)aDetermines if a circuit is reducible by checking if its subcircuits are scalar values. Parameters ========== circuit : Gate tuple A tuple of Gates representing a circuit. The circuit to check if a gate identity is contained in a subcircuit. nqubits : int The number of qubits the circuit operates on. begin : int The leftmost gate in the circuit to include in a subcircuit. end : int The rightmost gate in the circuit to include in a subcircuit. Examples ======== Check if the circuit can be reduced: >>> from sympy.physics.quantum.identitysearch import is_reducible >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> is_reducible((x, y, z), 1, 0, 3) True Check if an interval in the circuit can be reduced: >>> is_reducible((x, y, z), 1, 1, 3) False >>> is_reducible((x, y, y), 1, 1, 3) True rkFT)reversedrangerV)r2rbeginendcurrent_circuitndx next_gates rHrrsPJOeS)*CL $,8 _gu =  rIc ||dkr t|}|}tdg}t}t|dkDr|j}|D]u}||fz} t | |dt| } t | ||r&t || s| s|jt| St| |ksb| re|j| wt|dkDr|S)aAConstructs a set of gate identities from the list of possible gates. Performs a breadth first search over the space of gate identities. This allows the finding of the shortest gate identities first. Parameters ========== gate_list : list, Gate A list of Gates from which to search for gate identities. nqubits : int The number of qubits the quantum circuit operates on. max_depth : int The longest quantum circuit to construct from gate_list. identity_only : bool True to search for gate identities that reduce to identity; False to search for gate identities that reduce to a scalar. Examples ======== Find a list of gate identities: >>> from sympy.physics.quantum.identitysearch import bfs_identity_search >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> bfs_identity_search([x], 1, max_depth=2) {GateIdentity(X(0), X(0))} >>> bfs_identity_search([x, y, z], 1) {GateIdentity(X(0), X(0)), GateIdentity(Y(0), Y(0)), GateIdentity(Z(0), Z(0)), GateIdentity(X(0), Y(0), Z(0))} Find a list of identities that only equal to 1: >>> bfs_identity_search([x, y, z], 1, identity_only=True) {GateIdentity(X(0), X(0)), GateIdentity(Y(0), Y(0)), GateIdentity(Z(0), Z(0))} rrkr ) rUrrwrxrrVrrmrrn) gate_listr max_depthr3id_onlyrridsrr new_circuitcircuit_reducibles rHrrsTIN N G 2$KE %C u:>--/" *I)YL8K!-['-.K0@!B  !gw?!#{3) k23k"Y.' [)! * u:>* JrIct|}d}t|D]}|td|dz }||fz}t||d}|r|SdS)zRandomly selects numgates from gate_list and checks if it is a gate identity. If the circuit is a gate identity, the circuit is returned; Otherwise, None is returned. rkrr FN)rUrrrV)rnumgatesr gate_sizer2ir is_scalars rHrrDscIIG 8_)gaQ78 YL()!'59I7)T)rI)gdy=rN)F)NF)' collectionsrsympy.core.randomrsympy.externalrsympy.core.basicrsympy.core.mulrsympy.core.numbersr r sympy.core.powerr sympy.core.singletonr sympy.physics.quantum.representr sympy.physics.quantum.daggerr__all__r"rrrrVrRr]rarerirrrrrrrrkrIrHrs%("3 "5/ 7gj8*-EFUGp-H^%.1!0f0f0f0fNbL^:!5:!z$N/d7;K\*rI