From 28aea6c797158f20c7391bfe63abf2534a75e6d9 Mon Sep 17 00:00:00 2001 From: Panadestein Date: Mon, 2 Sep 2024 16:55:03 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20Panadest?= =?UTF-8?q?ein/blog@92bf925761c9a3cab2350e75fff9b6fccfcdf731=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hf.html | 24 ++++---- index.html | 1 - qbqn.html | 40 +++++++------- rollim.html | 156 ++++++++++++++++++++++++++++++++++++++++++---------- si.html | 12 ++-- spodat.html | 30 +++++----- 6 files changed, 181 insertions(+), 82 deletions(-) diff --git a/hf.html b/hf.html index 3ed22e2..40f86ac 100644 --- a/hf.html +++ b/hf.html @@ -234,9 +234,9 @@

Helonium's Hartree-Fock program

-
-

Exordium

-
+
+

Exordium

+

We will implement the Hartree-Fock1 program from the classic Szabo-Ostlund text, a staple in quantum chemistry. If you have any experience in the field, chances are you know it well. @@ -270,9 +270,9 @@

Exordium

-
-

STO-3G

-
+
+

STO-3G

+

Basis sets are used to transform the PDEs into linear algebra problems. Physical intuition suggests that Slater type orbitals4 should be a good choice for our Hamiltonian. However, the computation of the integrals @@ -289,9 +289,9 @@

STO-3G

-
-

Electronic integrals

-
+
+

Electronic integrals

+

Constructing the integrals' tensor is complicated6 and is the main reason for the poor scaling of electronic structure methods. The \(1s\) orbitals are the simplest case, and here two types of integrals @@ -345,9 +345,9 @@

Electronic integrals

-
-

SCF

-
+
+

SCF

+

Compare the electronic energy with the one computed using the original F66 program.

diff --git a/index.html b/index.html index df12d00..4665bbb 100644 --- a/index.html +++ b/index.html @@ -217,7 +217,6 @@
  • BQN's Quantum Noise
  • Songs to pave the seasons
  • Helonium's Hartree-Fock program
  • -
  • Scheming a mise-en-abîme in BQN
  • diff --git a/qbqn.html b/qbqn.html index 6cc3b43..824fbae 100644 --- a/qbqn.html +++ b/qbqn.html @@ -202,9 +202,9 @@

    BQN's Quantum Noise

    -
    -

    Preamble

    -
    +
    +

    Preamble

    +

    We will implement and test a compact quantum interpreter in the BQN1 programming language. Initially, we import the necessary system functions and define a 1-modifier for handling @@ -212,7 +212,7 @@

    Preamble

    -
    Sin‿Cos‿GCD ← •math
    +
    Sin‿Cos‿GCD ← •math
     U ← •rand.Range
     _cp ← {(-´𝔽¨)⋈(+´𝔽¨)⟜⌽}
     
    @@ -226,16 +226,16 @@ 

    Preamble

    -
    -

    Interpreter

    -
    +
    +

    Interpreter

    +

    The (400 chars2) quantum interpreter is based on references arXiv:1711.02086 and arXiv:1608.03355. For simplicity, we always measure at the end of the execution:

    -
    Q ← {𝕊qb‿sc‿r:
    +
    Q ← {𝕊qb‿sc‿r:
       wf ← (1⌾⊑⋈⊢)⥊⟜0 2⋆qb
       M‿K ← ⟨+˝∘×⎉1‿∞ _cp, {1𝕊𝕩:𝕩; 𝕨𝕊1:𝕨; 𝕨∾∘×⟜<_cp𝕩}⟩
       E ← {0𝕊𝕩:1; K⍟(𝕨-1)˜𝕩}
    @@ -251,9 +251,9 @@ 

    Interpreter

    -
    -

    Shor's algorithm

    -
    +
    +

    Shor's algorithm

    +

    As a test case, we employ the quantum circuit of Shor's algorithm for the number fifteen and base eleven, following references @@ -264,7 +264,7 @@

    Shor's algorithm

    -
    n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
    +
    n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
     
     sc ← ⟨
       ⟨0⟩‿g.h ⋄ ⟨1⟩‿g.h ⋄ ⟨2⟩‿g.h
    @@ -282,7 +282,7 @@ 

    Shor's algorithm

    -
    C >+˝{Q qb‿sc‿𝕩}¨ r
    +
    C >+˝{Q qb‿sc‿𝕩}¨ r
     
    @@ -296,9 +296,9 @@

    Shor's algorithm

    -
    -

    Epilogue

    -
    +
    +

    Epilogue

    +

    Why the hieroglyphs, you may ask? The tacit and functional style, coupled with numerous combinators, makes programming feel like solving a fun algebraic puzzle rather than drafting a manifesto. @@ -317,7 +317,7 @@

    Epilogue

    -
    +
     ⟨ 44 64 ⟩
     
    @@ -330,7 +330,7 @@

    Epilogue

    -
    +
     ┌─                                                                                                                                                                                 
     ╵ '-' '´' '¨' '⋈' '+' '⟜' '⌽' '⊢' '≢' '⥊' '<' '=' '⌜' '˜' '↕' '∾' '○' '⌾' '⊸' '⊑' '÷' '√' '⊏' '⋆' '˝' '∘' '×' '⎉' '≡' '⊣' '⌊' '⁼' '≠' '⍟' '◶' '↓' '¬' '∊' '/' '»' '∨' '`' '>' '⍒'  
       8   8   10  5   8   3   6   7   1   5   9   6   3   12  6   5   2   5   7   9   5   1   1   5   4   8   5   1   3   3   1   1   5   1   2   1   1   1   1   1   1   2   3   1    
    @@ -351,7 +351,7 @@ 

    Epilogue

    While the interpreter's performance is not particularly optimized, here is a comparison with the equivalent Common Lisp code:

    -
    +
     Benchmark 1: cbqn -f ./bqn/q.bqn
       Time (mean ± σ):      5.468 s ±  0.077 s    [User: 5.427 s, System: 0.005 s]
       Range (min … max):    5.358 s …  5.535 s    5 runs
    @@ -374,7 +374,7 @@ 

    Epilogue

    -
    +
     Got 25361 samples
     (REPL): 25361 samples:
          2│  Q ← {𝕊qb‿sc‿r:
    diff --git a/rollim.html b/rollim.html
    index 99d445e..0197a95 100644
    --- a/rollim.html
    +++ b/rollim.html
    @@ -235,51 +235,99 @@
     

    A coding impromptu

    -A rolling collection of algorithms I like, implemented in BQN. +A rolling collection of algorithms I like, implemented in BQN. It serves as my personal complement +to the bqncrate. Extrapolating Perlis' remark1, it's likely that a group of 50 individuals +would devise 35 to 40 distinct solutions to even the simplest problem in BQN. Therefore, I will frequently +juxtapose my implementations with those of seasoned BQNators, acknowledging their contributions +in footnotes.

    -
    -

    Z algorithm

    -
    +
    +

    Z algorithm

    +

    This is a very efficient procedure that finds prefix strings in linear time. The imperative implementation reads:

    -
    ZI ← {
    -  l‿r‿z ← 0‿0‿(0¨𝕩)
    -  {z ⊏˜⌾(r<◶(r-𝕩+1⌊z⊏˜𝕩-l)‿0)𝕩}¨ ↕≠𝕩
    +
    ZI ← {𝕊s:
    +  l‿r‿z ← 0⚇0 0‿0‿s
    +  z ⊣ {
    +    v ← r(⊢1⊸+•_while_{(𝕩+𝕨)<≠s ? =´⟨𝕩,𝕩+𝕨⟩⊑¨<s ; 0}<◶({z⊑˜𝕩-l}⌊-+1)‿0)𝕩
    +    r <◶@‿{𝕊: l↩𝕩-v+1 ⋄ r↩𝕩} 𝕩+v-1
    +    z v⌾(𝕩⊸⊑)↩
    +  }¨ ↕≠s
     }
     ZI "abacabadabacaba"
     
    +
    +⟨ 15 0 1 0 3 0 1 0 7 0 1 0 3 0 1 ⟩
    +
    + + +

    +Two algorithmic improvements were proposed2 for the function above, namely only iterate +over indices where the character found is equal to the first character, and only search +to extend the count if it goes up to the end of r: +

    +
    -
    int x = 0, y = 0;
    -for (int i = 1; i < n; i++) {
    -  z[i] = (y < i) ? 0 : min(y-i+1,z[i-x]);
    -  while (i+z[i] < n && s[z[i]] == s[i+z[i]]) z[i]++;
    -  if (i+z[i]-1 > y) {
    -    x = i; y = i+z[i]-1;
    -  }
    +
    ZFun ← {𝕊s:
    +  CountEq ← { 1⊸+•_while_((≠𝕨)⊸≤◶⟨⊑⟜𝕨≡⊑⟜𝕩,0⟩) 0 }
    +  l←r←0 ⋄ Ulr ← {(r⌈↩𝕨+𝕩)>r ? l↩𝕨 ⋄ 𝕩; 𝕩}
    +  SearchEq ← ⊣ Ulr ⊢ + + CountEq○(↓⟜s) ⊢
    +  Set ← {i𝕊𝕩: ((r-i) (i SearchEq 0⌈⊣)⍟≤ (i-l)⊑𝕩)⌾(i⊸⊑) 𝕩 }
    +  (⌽1↓/⊑⊸=s) Set´˜ ↑˜≠s
     }
     

    -And the array version: +An array version which I find more beautiful, but has quadratic time complexity is: +

    + +
    +
    ZAQ ← ¯1↓↓(+´·∧`⊣=≠⊸↑)¨<
    +ZAQ "abacabadabacaba"
    +
    +
    + +
    +⟨ 15 0 1 0 3 0 1 0 7 0 1 0 3 0 1 ⟩
    +
    + + +

    +I also came up with a cubic solution, but as we will see it can be made quadratic: +

    + +
    +
    ZAC ← (+´∧`)¨<=↕∘≠{«⍟𝕨𝕩}⌜<
    +ZAC "abacabadabacaba"
    +
    +
    + +
    +⟨ 15 0 1 0 3 0 1 0 7 0 1 0 3 0 1 ⟩
    +
    + + +

    +Further improvements where proposed by2, 3, rendering the above solutions into:

    -
    ZA ← 1
    -ZA "abacabadabacaba"
    +
    ZAC ← (+´∧`)¨≠↑↓=⌽∘↑
    +ZAC ← (+´∧`)¨<=«⍟(↕∘≠)
     
    -
    -

    Longest increasing sub-sequence

    -
    +
    +

    Longest increasing sub-sequence

    +

    This problem can be solved in \(O(n\log n)\) using dynamic programming. Here is an imperative implementation which is quadratic, but can be optimized: @@ -295,8 +343,13 @@

    Longest increasing sub-sequence

    +
    +⟨ 4 4 1 ⟩
    +
    + +

    -A more elegant array and tacit solution was crafted by Dzaima and Marshall: +A more elegant array and tacit solution was crafted by2, 3:

    @@ -305,15 +358,20 @@

    Longest increasing sub-sequence

    +
    +⟨ 4 4 1 ⟩
    +
    + +

    In case you are wondering like I did, the minus one there is to make the bins comparison strictly increasing.

    -
    -

    N-queens problem

    -
    +
    +

    N-queens problem

    +

    This problem is the archetypal example of backtracking. Initially, I tried to solve it using a function to place the queens in the full board, hoping that it would lead to a @@ -321,10 +379,23 @@

    N-queens problem

    -
    8 {n𝕊i‿j: ((∨⌜´0⊸=)∨(0=-⌜´)∨0=+⌜´) j‿i-¨<↕n} 2‿3
    +
    8 {((∨⌜´0⊸=)∨(0=-⌜´)∨0=+⌜´) 𝕩-¨<↕𝕨} 2‿3
     
    +
    +┌─                 
    +╵ 0 1 0 1 0 1 0 0  
    +  0 0 1 1 1 0 0 0  
    +  1 1 1 1 1 1 1 1  
    +  0 0 1 1 1 0 0 0  
    +  0 1 0 1 0 1 0 0  
    +  1 0 0 1 0 0 1 0  
    +  0 0 0 1 0 0 0 1  
    +  0 0 0 1 0 0 0 0  
    +                  ┘
    +
    +

    This resulted in a more complicated algorithm, so I decided to go for the classical Wirth implementation: @@ -349,11 +420,40 @@

    N-queens problem

    - +
    +1
    +
    + + +

    +And of course, in the implementation above I could have used a single array instead of three, +but I find the resulting validation and position functions very aesthetic the way they are. +

    +
    +

    Footnotes:

    +
    + +
    1

    +Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English, +from jsofware's papers collection. +

    + +
    2

    +Marshall Lochbaum, the BQN creator. +

    + +
    3

    +dzaima, the CBQN developer. +

    + +
    + +
    +
    diff --git a/si.html b/si.html index 609544e..58a5b44 100644 --- a/si.html +++ b/si.html @@ -202,9 +202,9 @@

    Scheming a mise-en-abîme in BQN

    -
    -

    Prelude

    -
    +
    +

    Prelude

    +

    We will build and interpreter for a subset of the Scheme programming language, following a Norvig's essay. An alternative reference would @@ -213,9 +213,9 @@

    Prelude

    -
    -

    A Lisp quine

    -
    +
    +

    A Lisp quine

    +

    Given the title of this post, I couldn't think of a better test for our interpreter than this one: diff --git a/spodat.html b/spodat.html index baf1fce..dcfc8f6 100644 --- a/spodat.html +++ b/spodat.html @@ -206,9 +206,9 @@

    Songs to pave the seasons

    I have analyzed my Spotify data1 for the period 2016-2024. The results accurately represent my actual music taste.

    -
    -

    Technical details

    -
    +
    +

    Technical details

    +

    This is a suitable task for an array language, so I rely on BQN which is my favorite one: @@ -240,15 +240,15 @@

    Technical details

    -
    -

    Top songs

    -
    +
    +

    Top songs

    +
    s Q spd	
     
    -
    +
     ┌─                                                     
     ╵ 1  "Countless Skies"                                 
       2  "Divertimento I, K.136: Allegro"                  
    @@ -274,15 +274,15 @@ 

    Top songs

    -
    -

    Top artists

    -
    +
    +

    Top artists

    +
    a Q spd
     
    -
    +
     ┌─                              
     ╵ 1  "Opeth"                    
       2  "Wolfgang Amadeus Mozart"  
    @@ -308,9 +308,9 @@ 

    Top artists

    -
    -

    Bonus: Opeth anthology

    -
    +
    +

    Bonus: Opeth anthology

    +

    This is the Opeth album I would recommend to anyone. The query function needs to be modified a bit for generating it. But before that, let's look at the official discography: @@ -339,7 +339,7 @@

    Bonus: Opeth anthology

    -
    +
     ┌─                             
     ╵ 1 "Ghost of Perdition"       
       2 "River"