I'm using the Quantum Computing library with this example:
Needs["Quantum`Computing`"];
SetComputingAliases[];
m[a_] = {{Cos[a], -I Sin[a]}, {-I Sin[a], Cos[a]}};
mq = m[myangle] //MatrixQuantum
(* Fails both times *)
PauliExpand[mq]
Assuming[myangle ∈ Reals, PauliExpand[mq]]
Error is:
PauliExpand::nonunit:: PauliExpand can only expand unitary operators
This happens because a function within the Quantum Computing library called unitaryQ
uses Dynamic Programming to cache results. If it's evaluated without assumptions first then that result sticks around. Further evaluations replay the cached definition, so adding assumptions later that would allow the function to evaluate successfully does not help.
Conversely, if the kernel is reset and the functions are run this way round, the expected results are returned:
(*Succeeds both times*)
Assuming[myangle ∈ Reals, PauliExpand[mq]]
PauliExpand[mq]
Here's the part of the library in Computing.m
that implements the unitaryQ
function:
(* Notice the delayed assignment *)
$QuantumMaxUnitaryDefinitions:=$QuantumMaxCachedDefinitions;
uStoredDef=0;
autoresetUnitaryQ[]:=
Module[{},
If[uStoredDef>$QuantumMaxUnitaryDefinitions,
resetUnitaryQ[] ];
uStoredDef++
];
resetUnitaryQ[]:=
Module[{},
Clear[unitaryQ];
uStoredDef=0;
unitaryQ[m_List?MatrixQ] :=
( autoresetUnitaryQ[];
unitaryQ[m] = (* dynamic programing *)
Simplify[Conjugate@[email protected] ==IdentityMatrix@Length@m] );
];
(* First Time: *)
resetUnitaryQ[];
My question is: are there any support functions to help peek inside a module and see any cached assignments and perhaps clear them? Or is it all locked away as an implementation detail and the only option is a kernel reset if an undesirable definition is cached?
Additionally, are there any patterns that could be followed in the unitaryQ
definition (which would require changes to the library) that would either:
- Take assumptions into account when caching definitions, allowing the two calls in my code to be evaluated differently.
or
- Only cache a definition if the function evaluates successfully. This would not resolve issues if different assumptions were specified.
?