TransWikia.com

How do I skip to the end of a loop when a compiled function yields an error?

Mathematica Asked on January 25, 2021

This is something of a follow-up to my question In an iterative process, how does one simply bypass a step producing an error?
which I have been so far (thick-headedly) been unsuccessful in incorporating into my specific code.

The code at the end runs a three-step loop with the index i95 running from 16,630,332, to 16,630,334. At 16,630,333 a compiled function error is generated. I then want to immediately Goto[eax] at the end of the loop, skipping the intermediate calculations, and bump the index up to 16,630,334.
The function in question is

t4 = Compile[{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, l1, l2, l3, l4}, Haar s3,CompilationTarget -> "C",RuntimeAttributes -> {Listable}, Parallelization -> True];

The full code (quickly executing) that I am trying to implement (the function is located about 2/3 of the way down) is

        S = {{{0, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 
        0}}, {{0, -I, 0, 0}, {I, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 
        0}}, {{1, 0, 0, 0}, {0, -1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 
        0}}, {{0, 0, 1, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 
        0}}, {{0, 0, -I, 0}, {0, 0, 0, 0}, {I, 0, 0, 0}, {0, 0, 0, 
        0}}, {{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 
        0}}, {{0, 0, 0, 0}, {0, 0, -I, 0}, {0, I, 0, 0}, {0, 0, 0, 
        0}}, {{1/Sqrt[3], 0, 0, 0}, {0, 1/Sqrt[3], 0, 0}, {0, 
        0, -2/Sqrt[3], 0}, {0, 0, 0, 0}}, {{0, 0, 0, 1}, {0, 0, 0, 0}, {0,
         0, 0, 0}, {1, 0, 0, 0}}, {{0, 0, 0, -I}, {0, 0, 0, 0}, {0, 0, 0, 
        0}, {I, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 
        1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, -I}, {0, 0, 0, 0}, {0, I, 0, 
        0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 
        0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, -I}, {0, 0, I, 
        0}}, {{1/Sqrt[6], 0, 0, 0}, {0, 1/Sqrt[6], 0, 0}, {0, 0, 
        1/Sqrt[6], 0}, {0, 0, 0, -Sqrt[3/2]}}}; 
    f1[x_, y_] := MatrixExp[I S[[x]] y]; U = 
     f1[3, a1].f1[2, a2].f1[3, a3].f1[5, a4].f1[3, a5].f1[10, a6].f1[3, 
       a7].f1[2, a8].f1[3, a9].f1[5, a10].f1[3, a11].f1[2, a12]; Haar = 
     Cos[a4]^3 Cos[a6] Cos[a10] Sin[2 a2] Sin[a4] Sin[a6]^5 Sin[
       2 a8] Sin[a10]^3 Sin[
       2 a12] ((l1 - l2)^2 (l1 - l3)^2 (l2 - l3)^2 (l1 - l4)^2 (l2 - 
         l4)^2 (l3 - l4)^2)/ Sqrt[l1 l2 l3 l4] ; X = 
     Compile[{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12}, U,
      RuntimeAttributes -> {Listable}, 
      Parallelization -> True]; s3 = {Sqrt[l1 l2 l3 l4], 
      64/((l1 + l2) (l1 + l3) (l2 + l3) (l1 + l4) (l2 + l4) (l3 + 
         l4)), ((l1 + l2) (l1 + l3) (l2 + l3) (l1 + l4) (l2 + l4) (l3 + 
         l4))/(64 l1^3 l2^3 l3^3 l4^3), ((Log[l1] - Log[l2]) (Log[l1] - 
           Log[l3]) (Log[l2] - Log[l3]) (Log[l1] - Log[l4]) (Log[l2] - 
           Log[l4]) (Log[l3] - Log[l4]))/((l1 - l2) (l1 - l3) (l2 - 
           l3) (l1 - l4) (l2 - l4) (l3 - l4)), 1/(
      l1^(3/2) l2^(3/2) l3^(3/2) l4^(3/2)), 
      4096/((Sqrt[l1] + Sqrt[l2])^2 (Sqrt[l1] + Sqrt[l3])^2 (Sqrt[l2] + 
         Sqrt[l3])^2 (Sqrt[l1] + Sqrt[l4])^2 (Sqrt[l2] + Sqrt[
         l4])^2 (Sqrt[l3] + Sqrt[
         l4])^2), ((l1 + l2) (l1 + l3) (l2 + l3) (l1 + l4) (l2 + l4) (l3 +
            l4) (Log[l1] - Log[l2]) (Log[l1] - Log[l3]) (Log[l2] - 
           Log[l3]) (Log[l1] - Log[l4]) (Log[l2] - Log[l4]) (Log[l3] - 
           Log[l4]))/(64 l1^(3/2) (l1 - l2) l2^(
         3/2) (l1 - l3) (l2 - l3) l3^(
         3/2) (l1 - l4) (l2 - l4) (l3 - l4) l4^(
         3/2)), (4096 (l1 + l2) (l1 + l3) (l2 + l3) (l1 + l4) (l2 + 
           l4) (l3 + l4))/((l1^2 + 6 l1 l2 + l2^2) (l1^2 + 6 l1 l3 + 
           l3^2) (l2^2 + 6 l2 l3 + l3^2) (l1^2 + 6 l1 l4 + l4^2) (l2^2 + 
           6 l2 l4 + l4^2) (l3^2 + 6 l3 l4 + l4^2)), ((l1 + l2) (l1 + 
           l3) (l2 + l3) (l1 + l4) (l2 + l4) (l3 + l4) (Log[l1] - 
           Log[l2])^2 (Log[l1] - Log[l3])^2 (Log[l2] - 
           Log[l3])^2 (Log[l1] - Log[l4])^2 (Log[l2] - 
           Log[l4])^2 (Log[l3] - Log[l4])^2)/(64 (l1 - l2)^2 (l1 - 
           l3)^2 (l2 - l3)^2 (l1 - l4)^2 (l2 - l4)^2 (l3 - l4)^2), (
      E^6 (l1/l2)^(l1/(-l1 + l2)) (l1/l3)^(
       l1/(-l1 + l3)) (l2/l3)^(-1 + l2/(-l2 + l3)) (l1/l4)^(
       l1/(-l1 + l4)) (l2/l4)^(l2/(-l2 + l4)) (l3/l4)^(l3/(-l3 + l4)))/(
      l3^3 l4^3)}; t4 = 
     Compile[{a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, l1, l2, 
       l3, l4}, Haar s3,CompilationTarget -> "C",
      RuntimeAttributes -> {Listable}, Parallelization -> True]; hw = 
     TimeUsed[]; ay = 0; by = 0; U2 = {}; qv1 = Array[ 0 &, 10]; qv2 = 
     Array[ 0 &, 10]; sp2 = x /. Solve[x^(16) == x + 1, x][[2]]; Print[
     N[sp2]]; G = Array[1, 15]; Do[
     G[[i]] = FractionalPart[N[1/2 + i95/sp2^i]], {i, 1, 15}]; G1 = 
     Take[G, {1, 12}]; H1 = Take[G, {13, 15}]; Do[
     Print[i95]; {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12} = 
      G1 {Pi, Pi/2, Pi, Pi/2, Pi, Pi/2, Pi, Pi/2, Pi, Pi/2, Pi, 
        Pi/2}; {l1, l2, l3, l4} = Differences[Join[{0}, Sort[H1], {1}]]; 
     s4 = t4[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, l1, l2, 
       l3, l4]; qv1 = qv1 + s4; 
     V = X[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12]; 
     If[Re[Det[
         ArrayFlatten[
          Transpose[
           Partition[
            V.DiagonalMatrix[{l1, l2, l3, l4}].ConjugateTranspose[V], {2, 
             2}]]]]] > 0, qv2 = qv2 + s4; ay = ay + 1]; 
     If[Mod[i95, 1000000] == 0, 
      Print[{i95, ay, by, TimeUsed[] - hw, "Probs", {ay/i95, N[ay/i95]}, 
        qv2/qv1}]; hw = TimeUsed[]; 
      Print[{(qv2[[1]]/qv1[[1]])/(8/33), (qv2[[2]]/qv1[[2]])/(25/
           341), (qv2[[5]]/qv1[[5]])/(1 - 256/(27 Pi^2))}]; {i95, ay, by, 
        qv1, qv2} >> OperatorMonotoneTwoQubitsCampusSave; 
      U2 = Join[U2, {{qv1, qv2}}]; 
      U2 >> OperatorMonotoneTwoQubitsCampusSave]; 
     Label[eax], {i95, 16630332, 16630334}];

One Answer

Well, in the specific notation of the program the code

s4 = Check[t4[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, l1, l2, l3,l4], err]; If[s4 == err,Goto[eax]];

where t4[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, l1, l2, l3,l4] is the compiled function and Label[eax] is the statement at the end of the loop, succeeds in the task requested.

I still don't see specifically how to use either Throw and Catch or GeneralUtilities`WithMessageHandler' to do so. (But until this question, I was unfamiliar with these commands, as well as with Check.)

Answered by Paul B. Slater on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP