1
$\begingroup$

I am attempting to evaluate a notebook with the following code via the command line

Z = PauliMatrix[3];
X = PauliMatrix[1];
Y = PauliMatrix[2];
id = PauliMatrix[0];
spinRaise = (X + I Y)/2;
spinLower = (X - I Y)/2;
pauli0 = 1/2 (id - (Z/(2 + 1)));
zero = {1, 0};
one = {0, 1};
\[Sigma]p = (1/2) (X + I Y);
\[Sigma]m = (1/2) (X - I Y);
\[Sigma]0 = (1/2) (id - Z/(2 n + 1));
\[Sigma]z = {{1, 0}, {0, -1}};

fragState = {{1/2, 1/2}, {1/2, 1/2}};
\[Rho]initialE = KroneckerProduct[fragState, fragState, fragState];
(*systemInitial={{1/4,1/4,0,0},{1/4,1/4,0,0},{0,0,0,0},{0,0,0,1/2}}*)

systemInitial = {{1/2, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1/4, 1/4}, {0, 
    0, 1/4, 1/4}};
(*systemInitial={{1/2,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,1/2}};*)
\
(*systemInitial={{1/3,0,0,0},{0,0,0,0},{0,0,1/3,1/3},{0,0,1/3,1/3}};*)

totalSysInitial = 
  KroneckerProduct[N[systemInitial], N[\[Rho]initialE]];
subsystem2Measure1 = 
  KroneckerProduct[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}, 
   Conjugate[{Cos[\[Theta]], Exp[I \[Phi]]*Sin[\[Theta]]}]];
subsystem2Measure2 = 
  KroneckerProduct[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}, 
   Conjugate[{Exp[-I \[Phi]]*Sin[\[Theta]], -Cos[\[Theta]]}]];
postMeasuredState1 = 
  dTraceSystem[
   Flatten[Map[
      KroneckerProduct @@ # &, {{subsystem2Measure1, 
        IdentityMatrix[2]}}], 1].systemInitial.Flatten[
     Map[KroneckerProduct @@ # &, {{subsystem2Measure1, 
        IdentityMatrix[2]}}], 1], {1}, 2];
postMeasuredState2 = 
  dTraceSystem[
   Flatten[Map[
      KroneckerProduct @@ # &, {{subsystem2Measure2, 
        IdentityMatrix[2]}}], 1].systemInitial.Flatten[
     Map[KroneckerProduct @@ # &, {{subsystem2Measure2, 
        IdentityMatrix[2]}}], 1], {1}, 2];
probMeasure1 = 
  Simplify[ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{postMeasuredState1, 
          IdentityMatrix[2]}}], 1].systemInitial.Flatten[
       Map[KroneckerProduct @@ # &, {{postMeasuredState1, 
          IdentityMatrix[2]}}], 1]]]];
probMeasure2 = 
  Simplify[ComplexExpand[
    Tr[Flatten[
       Map[KroneckerProduct @@ # &, {{postMeasuredState2, 
          IdentityMatrix[2]}}], 1].systemInitial.Flatten[
       Map[KroneckerProduct @@ # &, {{postMeasuredState2, 
          IdentityMatrix[2]}}], 1]]]];
normPost1 = postMeasuredState1/probMeasure1;
normPost2 = postMeasuredState2/probMeasure2;
eigPost1 = 
  Eigenvalues[normPost1 + 10^-15 IdentityMatrix[Length[normPost1]]];
entropyPost1 = 
  Simplify[ComplexExpand[-Sum[
      eigPost1[[j]] Log[2, eigPost1[[j]]], {j, 1, Length[eigPost1]}]]];
eigPost2 = 
  Eigenvalues[normPost2 + 10^-15 IdentityMatrix[Length[normPost2]]];
entropyPost2 = 
  Simplify[ComplexExpand[-Sum[
      eigPost2[[j]] Log[2, eigPost2[[j]]], {j, 1, Length[eigPost2]}]]];
eigSub2 = 
 Eigenvalues[
  dTraceSystem[sysInitial, {1}, 2] + 
   10^-15 IdentityMatrix[Length[dTraceSystem[sysInitial, {1}, 2]]]]
entropyPost2 = 
  Simplify[ComplexExpand[-Sum[
      eigSub2[[j]] Log[2, eigSub2[[j]]], {j, 1, Length[eigSub2]}]]];
entropyPost2 - 
 NMinimize[{probMeasure1*entropyPost1 + 
    probMeasure2*entropyPost2, {\[Theta] \[Element] Reals, \[Theta] <=
      2*\[Pi], \[Phi] \[Element] Reals, 
    0 <= \[Phi] <= 2*\[Pi]}}, {\[Theta], \[Phi]}]

I have tried math -script <notebook.nb, math -run < notebook.nb, the same again using a .wls file instead (yes, I changed the cells to initialisation cells), and also a .m file with math -run "<<notebook.m". Even changed the cells from Input to Code, per this post. There is no change to the files in any of these cases, and the runtime doesn't seem to be what it should be either. Sometime it's instant, others a few seconds. The only one that actually appears to output something to the command line, not even the file, is math -run < notebook.nb

All I want to do is take the notebook file, evaluate it via the command line, and have those evaluations appear in the notebook. I have looked at a myriad of posts, including this one, but I can't get it to work.

$\endgroup$
2
  • $\begingroup$ dTraceSystem is not defined in your code. $\endgroup$ Mar 5, 2022 at 13:48
  • $\begingroup$ Yes, its a long function, and has no bearing on the execution of this code on the command line, so I omitted it. $\endgroup$ Mar 7, 2022 at 10:35

1 Answer 1

1
$\begingroup$

The alternative version posted here works fine on Windows, on Mathematica 11.2 at least.

It's somewhat elaborate but it runs the notebook visibly so you can see it executing. The output cells are saved in the notebook.

A batch file runs a kernel which opens, runs & saves a notebook. The initial kernel monitors the notebook execution, useful for batch applications. This means the notebook requires a second kernel, in this case called "Kernel2", created via Kernel Configuration Options as a one-time manual setup. Kernel2 needs to be the notebook kernel, as indicated below. The batch file can be run from the command line or by the Windows Task Scheduler.

enter image description here

The above demo files are zipped and can be extracted from the image below. The files are preconfigured to run from C:\yourPath\

enter image description here

Zip retrieval

extractZip[file_String] := Uncompress@Last@StringSplit[
    URLFetch@file, "Embedded Zip:"]

writeZip[png_String, zip_String] := Module[{},
  outputstream = OpenWrite[FileNameJoin[{$InitialDirectory, zip}],
    BinaryFormat -> True];
  BinaryWrite[outputstream, extractZip@png];
  Close[outputstream]]

writeZip["https://i.stack.imgur.com/H5o8g.png", "files.zip"]

extractZip c/o Simon Woods

For batch runs

Using the same principles, there is also a very robust, albeit slow, batch runner here. Each notebook runs in its own session, and if a notebook hangs its slot will time-out and the next one will be processed. Great for overnight runs.

![enter image description here

writeZip["https://i.stack.imgur.com/c2zhD.png", "batchdemo.zip"]

Zip attachment code, for completeness

attachCode[file_String, expr_] := Block[{stream},
  stream = OpenAppend[file];
  WriteString[stream, "Embedded Zip:", Compress@expr];
  Close[stream]]

inputstream = OpenRead["C:\\temp\\batchdemo.zip", BinaryFormat -> True];
data = BinaryReadList[inputstream];
Close[inputstream];
attachCode["H:\\batchdemo.png", data]
$\endgroup$
9
  • $\begingroup$ So I am running this off of a remote cluster, and I don't think I am going to be able to get away with most of this. Is there not a simple command, to just run a notebook and have it save the evaluation? This all seems very complicated for what is, according to the doucmentation, something that is meant to be trivial. Edit: it's also not windows, it's a linux cluster ssh'ed into via macos. $\endgroup$ Mar 7, 2022 at 10:38
  • $\begingroup$ Also chris the post you linked, then expanded on is one I linked in my post. It did not work. $\endgroup$ Mar 7, 2022 at 10:48
  • $\begingroup$ Hi. If you run a notebook from a script it will execute but you'll need to export your results; it won't save the output cells in the notebook. If you want the output cells saved (as I did) the next simplest thing is to use a script to call a notebook (kernel) that opens a front end i.e. UseFrontEnd. You don't necessarily need two kernels - you can just place NotebookSave[] at the end of the evaluation notebook, but it's difficult to close everything after. You can use a set Pause, or monitor with a second kernel. $\endgroup$ Mar 7, 2022 at 10:59
  • $\begingroup$ Did you try the files.zip demo? It works on Windows on Mathematica 11.2. What are you running on? $\endgroup$ Mar 7, 2022 at 11:02
  • 1
    $\begingroup$ Hi, yes, in your nb file convert the cells to type Initialization Cell and save to create package .m. Then try and run that with WolframKernel or MathKernel. $\endgroup$ Mar 7, 2022 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.