
    \hk                     N   S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
JrJr  S SKJr  S SKJr  S S	KJr  S S
KJr  / SQr\" S5      r\" SSS/0S9rS!S jrS"S jr\(       a
  \(       a  \rO\rS rS rS rS rS rS#S jr S#S jr! " S S\5      r"S r#S r$  S$S jr%S  r&g)%    )deque)randint)import_module)BasicMul)Numberequal_valued)Pow)S)	represent)Dagger)	generate_gate_rulesgenerate_equivalent_idsGateIdentitybfs_identity_searchrandom_identity_searchis_scalar_sparse_matrixis_scalar_nonsparse_matrixis_degenerateis_reduciblenumpyscipyfromlistsparse)import_kwargsc                    [         (       a  [        (       d   [        [        U 6 USS9n[	        U[
        5      (       a  U(       a  US:H  $ S$ UR                  5       R                  5       n[         R                  UR                  U* :  UR                  U:  5      n[         R                  UR                  U* :  UR                  U:  5      n[         R                  USUR                  5      n[         R                  USUR                  5      n	U	[        S5      -  n	X-   n
U
R                  5       S   nU
R                  5       S   nX:H  nUR                  5       nU
S   S   nUS:X  a  g	X-  R                  5       n[!        S
U5      nUU:H  n[#        UR                  S-
  5      U:  n[#        UR                  5      U:  nU=(       a    UnU(       a  UOSn[%        U=(       a    U=(       a    U5      $ )ax  Checks 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)nqubitsformat   Tg        y              ?r   y                F   g      ?)npr   r   r   
isinstanceinttodensegetAlogical_andrealimagwherecomplexnonzeroalltracepowabsbool)circuitr   identity_onlyepsmatrixdense_matrix	bool_real	bool_imagcorrected_realcorrected_imagcorrected_denserow_indicescol_indicesbool_indicesis_diagonalfirst_elementtrace_of_correctedexpected_tracehas_correct_tracereal_is_oneimag_is_zerois_oneis_identitys                          \/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/quantum/identitysearch.pyr   r       s   , 2UUsG}g,.F
 	63+v{55 ~~',,. NN<#4#4t#;#/#4#4s#:<	 NN<#4#4t#;#/#4#4s#:<	 )S,2C2CD)S,2C2CD''"+5(9 &--/2%--/2 #1"&&('*1- Z'
 .;BBDQ..@ -,,s23c9=--.4- -f4KE$5E+FF    Nc                 L   [        [        U 6 US9n[        U[        5      (       a  U(       a  US:H  $ S$ UR	                  5       nU(       d  XTS   -  OUnU(       a  [        US   S5      OSnU[        SU5      :H  n[        UR                  5       =(       a    U=(       a    U5      $ )a  Checks 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
)r   r    Tr   r!   )	r   r   r#   r	   r.   r
   r/   r1   r?   )	r2   r   r3   r4   r5   matrix_traceadjusted_matrix_tracerG   rC   s	            rH   r   r   x   s    ( sG}g6F 	66""+v{55 ||~ )6 ".Qi!7&2 	 5Bl6!9a0t1SG_D  F%6F;H 	HrI   c                 p    [        U [        5      (       a  U R                  R                  $ U R                  $ N)r#   r   base
min_qubits)a_gates    rH   _get_min_qubitsrR      s+    &#{{%%%   rI   c                     [        U 5      S:  a&  U S   n[        [        U5      U4[        U5      S5      n[        U 5      S:  a'  W(       a   U S[        U 5       n[        W5      4U-   nXE4$ g)a_  Perform 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)))
r   Tr    Nlenis_scalar_matrixr   rR   )leftrightll_gatell_gate_is_unitarynew_left	new_rights         rH   ll_opr]      sw    F 	D	Aq'-G_g&(@$H 	D	A,#d)$G_&.	$$rI   c                     [        U 5      S:  a2  U [        U 5      S-
     n[        [        U5      U4[        U5      S5      n[        U 5      S:  a*  W(       a#  U S[        U 5      S-
   nU[        W5      4-   nXE4$ g)aa  Perform 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)))
r   r    TNrT   )rW   rX   lr_gatelr_gate_is_unitaryr[   r\   s         rH   lr_opra      s    F 	D	As4y1}%-G_g&(@$H 	D	A,#d)a-(VG_..	$$rI   c                     [        U5      S:  a&  US   n[        [        U5      U4[        U5      S5      n[        U5      S:  a'  W(       a   US[        U5       n[        W5      4U -   nXT4$ g)a`  Perform 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)), ())
r   Tr    NrT   )rW   rX   rl_gaterl_gate_is_unitaryr\   r[   s         rH   rl_opre     sw    F 	E
Q(-G_g&(@$H 	E
Q-!CJ'	7O%,$$rI   c                     [        U5      S:  a2  U[        U5      S-
     n[        [        U5      U4[        U5      S5      n[        U5      S:  a*  W(       a#  US[        U5      S-
   nU [        W5      4-   nXT4$ g)ab  Perform 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),))
r   r    TNrT   )rW   rX   rr_gaterr_gate_is_unitaryr\   r[   s         rH   rr_opri   M  s    F 	E
QE
Q'-G_g&(@$H 	E
Q-!CJN+	6'?,,$$rI   c                   ^	^
^ [        U [        5      (       a+  U(       a!  [        R                  [        R                  41$ S1$ [        U [        5      (       a  U R
                  n [        5       m
[        5       m[        U 5      m	U	U
U4S jnT
R                  U SS45        TR                  U S45        [        T
5      S:  ap  T
R                  5       u  p4n[        X45      nU" Xe5        [        X45      nU" Xe5        [        X45      nU" Xe5        [        X45      nU" Xe5        [        T
5      S:  a  Mp  U(       a8  [        5       nT H&  nUu  p4UR                  [	        U6 [	        U6 45        M(     UmT$ )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))}
) rk   c                    > U bD  U u  p#U T;  a  X24T;  a  TR                  U 5        US-   T:  a  TR                  XS-   4-   5        g g g )Nr    )addappend)new_ruleopsr[   r\   max_opsqueueruless       rH   process_new_rule-generate_gate_rules.<locals>.process_new_rule  sZ    "*Hu$))>e)K		(#Qw Xq
23 !  rI   rk   r   )r#   r	   r   Oner   argsr   setrU   rn   rm   popleftr]   ra   re   ri   )gate_seqreturn_as_mulsrt   rW   rX   rp   ro   	mul_rulesrulerq   rr   rs   s            @@@rH   r   r     s@   d (F##UUAEEN##:	Hc	"	"== GEEE(mG4 
LL(B"#	IIxn
e*q. ==?S %'%'%'%' e*q.  E	DKDMM3:sE{34  LrI   c                 x   [        U [        5      (       a  [        R                  1$ [        U [        5      (       a  U R
                  n [        5       n[        U 5      nU H9  nUu  pVUS:X  a  UR                  U5        M   US:X  d  M(  UR                  U5        M;     U(       a  S n[        [        Xr5      5      nU$ )al  Returns 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)}
rk   c                     [        U 6 $ rN   r   )id_seqs    rH   <lambda>)generate_equivalent_ids.<locals>.<lambda>Z  s    VrI   )
r#   r	   r   rv   r   rw   rx   r   rm   map)rz   r{   eq_ids
gate_rulesr}   lrconvert_to_muls           rH   r   r     s    j (F##w	Hc	"	"== UF$X.J7JJqM"WJJqM  4S01MrI   c                   d    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
S rS	rg
)r   i`  aO  Wrapper 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))}
c                     [         R                  " U /UQ76 n[        U6 Ul        [	        U5      Ul        [        U5      Ul        U$ rN   )r   __new__r   _circuitr   _rulesr   _eq_ids)clsrw   objs      rH   r   GateIdentity.__new__  s=    mmC'$'Dz(.
-d3
rI   c                     U R                   $ rN   )r   selfs    rH   r2   GateIdentity.circuit  s    }}rI   c                     U R                   $ rN   )r   r   s    rH   r   GateIdentity.gate_rules  s    {{rI   c                     U R                   $ rN   )r   r   s    rH   equivalent_idsGateIdentity.equivalent_ids  s    ||rI   c                     U R                   $ rN   )rw   r   s    rH   sequenceGateIdentity.sequence  s    yyrI   c                 ,    [        U R                  5      $ )z'Returns the string of gates in a tuple.)strr2   r   s    rH   __str__GateIdentity.__str__  s    4<<  rI   rk   N)__name__
__module____qualname____firstlineno____doc__r   propertyr2   r   r   r   r   __static_attributes__rk   rI   rH   r   r   `  sa    <        !rI   r   c                 8    U  H  nXR                   ;   d  M    g   g)a  Checks 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_identityan_ids      rH   r   r     s#    B 111  rI   c                 x    Sn[        [        X#5      5       H   nX   nU4U-   n[        XAS5      (       d  M     g   g)a[  Determines 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
rk   FT)reversedrangerV   )r2   r   beginendcurrent_circuitndx	next_gates          rH   r   r     sH    J Oe)*L	$,8 _u== + rI   c           	         Ub  US::  a  [        U 5      nUn[        S/5      n[        5       n[        U5      S:  a  UR                  5       nU  H  nXx4-   n	[	        XS[        U	5      5      n
[        XU5      (       a0  [        Xi5      (       d   U
(       d  UR                  [        U	6 5        M_  [        U	5      U:  d  Mp  U
(       a  My  UR                  U	5        M     [        U5      S:  a  M  U$ )a  Constructs 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))}
r   rk   r    )
rU   r   rx   ry   r   rV   r   rm   r   rn   )	gate_listr   	max_depthr3   id_onlyrr   idsr   r   new_circuitcircuit_reducibles              rH   r   r     s    T IN	N	G 2$KE %C u:>--/"I)L8K !-[-.K0@!B
 !w??!#33)k23k"Y.''[)! # u:>* JrI   c                     [        U 5      nSn[        U5       H  nU [        SUS-
  5         nXF4-   nM     [        XBS5      nU(       a  U$ S$ )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.
rk   r   r    FN)rU   r   r   rV   )r   numgatesr   	gate_sizer2   ir   	is_scalars           rH   r   r   D  sZ     IIG8_gaQ78	L(  !59I7)T)rI   )gdy=rN   )F)NF)'collectionsr   sympy.core.randomr   sympy.externalr   sympy.core.basicr   sympy.core.mulr   sympy.core.numbersr	   r
   sympy.core.powerr   sympy.core.singletonr   sympy.physics.quantum.representr   sympy.physics.quantum.daggerr   __all__r"   r   r   r   rV   rR   r]   ra   re   ri   r   r   r   r   r   r   r   rk   rI   rH   <module>r      s     % ( "  3   " 5 / 7gj8*-EFUGp-H^ %.1!0f0f0f0fNbL^:!5 :!z$N/d 7;K\*rI   