3
$\begingroup$

I am one of the maintainers of the quantum-utils library for Mathematica. We have been unable to sort out a problem that started with Mathematica 11 (on macOS, windows, and linux). The symptom is that when using a notebook that has imported the library, the system periodically freezes for about 5-15 seconds at a time, while the cursor turns to loading and the screen is greyed out. These instances seem to be correlated with moving the cursor within cells that contain functions defined in the library. This happens quite often and makes using the library quite infuriating.

At this point, we suspect the problem is with our documentation system, though we really don't know for sure. Our documentation system is centred around this answer given by @Pillsy. In summary, the usage message of a particular library function is stored as the contents of a cell in an ancillary notebook (labelled by a cell tag), which is set as the ::usage message of the function when the package is loaded.

In particular, we use this function:

LoadUsages[nbName_] := Module[{notebook, usageData, processCell, possibleBoxToString},
    notebook = Quiet[Get[nbName]];

    possibleBoxToString[elem_]:=If[StringQ[elem],
        elem,
        ToString[
        DisplayForm[elem]/.StyleBox[boxes_,"Input",args___]:>StyleBox[boxes,"Input",FontFamily->"Courier",args],
        StandardForm]
    ];

    processCell[contents_]:=If[
        StringQ[contents],
        contents,
        StringJoin@Map[possibleBoxToString,First@contents,1]
    ];

    If[\[Not](notebook === $Failed),
        usageData = Cases[notebook,
            Cell[contents_, ___, CellTags -> tag_, ___] /;
                StringMatchQ[tag, __ ~~ "::usage"] :>
            (
                StringReplace[tag, "::usage" -> ""] -> 
                processCell[contents]
            ),
        Infinity];

        UsageData @@ usageData,

        UsageData[]
    ]
]

to parse a .nb notebook function for all cells with usage tags, and this function

AssignUsage[codeSymb_Symbol->docSymb_Symbol, usageData_UsageData] := Module[{docName},
    docName = SymbolName @ Unevaluated[docSymb];
    If[FreeQ[usageData, (docName -> _)],
        Message[AssignUsage::nousg, usageData, HoldForm[docSymb]];
        MessageName[docSymb, "usage"] = "";
        $Failed,

        MessageName[codeSymb, "usage"] = docName /. List @@ usageData
    ]
]
AssignUsage[symb_Symbol, usageData_UsageData] := AssignUsage[symb->symb,usageData];

to assign the usage string to the function with name codeSymb. (These two functions are defined in src/QUDevTools.m of the library).

Then, near the top of the .m package file before the private context, one would run

$usagesDatabase = LoadUsages["someNotebook.nb"];
AssignUsage[MyFunction1, $usagesDatabase];
AssignUsage[MyFunction2, $usagesDatabase];

to set the usage messages of MyFunction1 and MyFunction2 to the respective contents of the cells with tags MyFunction1::usage and MyFunction2::usage in someNotebook.nb.

We suspect this to be the cause of the freezing because the symptoms show up when we are moving the cursor around inside cells. We could be way off base. I'm not sure why this would be the case since all of the notebook parsing is done in $usagesDatabase=.. so there should be no file lookups. Does anyone know enough about Mathematica 10 vs 11 that they could suggest a way to test this hypothesis? Or suggest a different cause?

PS: Please do not bring up Wolfram Workbench. I am aware of that product, and its benefits are sufficiently discussed elsewhere.

Edit: Although I cannot guarantee reproducibility because the symptoms are somewhat random, the following causes the problem for me with a fresh kernel of Mathematica 11.0.1.0 on linux.

  1. Install by downloading the repository, either through git clone or just as a zip. The main folder has Install.nb which when run will copy or symlink the package files into your $UserBaseDirectory.
  2. Populate a notebook with the lines of code shown below, one group of lines per cell. Run the notebook.
  3. Start moving the cursor around, especially in the last two cells. Add newlines, new text, delete old text, etc.

Code:

Needs["QuantumUtils`"]


H0 = 2 \[Pi] 0.1 Spin[Z][1/2];
H1 = 2 \[Pi] Spin[X][1/2];
H2 = 2 \[Pi] Spin[Y][1/2];
Udesired = TP[X];
A = {{-1, 1}, {-1, 1}};
dt = 0.05;
M = 20;


q0 = RandomSmoothPulse[dt, M, A];
PulsePlot[ToPulse@q0]
BlochPlot[
    States@PulseSim[H0, SimForm@ToPulse@q0, InitialState -> TP[U], 
    PollingInterval -> dt/2], BlochPlotJoined -> True]


q = FindPulse[q0, Udesired, 0.9999, A, {H1, H2}, H0, VerboseAscent -> True];
Print[MatrixForm@Last@Unitaries@PulseSim[H0, SimForm@q]];
PulsePlot[q]
BlochPlot[States@PulseSim[H0, SimForm@q, InitialState -> TP[U], 
   PollingInterval -> dt/2], BlochPlotJoined -> True]
$\endgroup$
  • $\begingroup$ Can you please give step by step instructions for reproducing the problem for those of us who never used this package? $\endgroup$ – Szabolcs Mar 31 '17 at 15:40
  • 1
    $\begingroup$ @Szabolcs thanks, updated. $\endgroup$ – Ian Hincks Mar 31 '17 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.