<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>John Hearn</title>
<link>https://johnhearn.github.io/notes.html</link>
<atom:link href="https://johnhearn.github.io/notes.xml" rel="self" type="application/rss+xml"/>
<description>Science and software</description>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Mon, 16 Mar 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Validating Euchre Bidding Thresholds with Monte Carlo Simulation</title>
  <dc:creator>John </dc:creator>
  <link>https://johnhearn.github.io/notes/2026-03-16-validating-euchre-bidding-thresholds/</link>
  <description><![CDATA[ 




<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p>A <a href="https://luke-fitz.github.io/predictive-modeling/game_of_euchre/">blog post on predictive modelling in Euchre</a> proposed a hand-scoring formula and a set of position-dependent bidding thresholds for UK Euchre. The scoring system assigns each hand a numeric value based on four components:</p>
<ul>
<li><strong>A</strong> — trump count bonus (0, 1, 2, 6, 9, or 12 for 0–5 trumps)</li>
<li><strong>B</strong> — individual trump card values (Benny=10, Right Bower=8, Left Bower=6, Ace=5, King=4, Queen/10=3, 9=2)</li>
<li><strong>C</strong> — offsuit aces (4 each) and kings (1 each)</li>
<li><strong>D</strong> — dealer penalty (Jack worth −5, Ace worth −2 in Round 1 only)</li>
</ul>
<p>The blog derived position-specific bid thresholds — for example, the Dealer needs only 16 points to bid in Round 1 (because they pick up the turn-up card), while the player to the Right of Dealer needs 31. It also proposed separate, higher thresholds for going alone.</p>
<p>The question I wanted to answer: <strong>are these thresholds actually optimal, or can we do better?</strong></p>
</section>
<section id="approach" class="level2">
<h2 class="anchored" data-anchor-id="approach">Approach</h2>
<p>All code for this project was generated interactively using <strong>Claude Opus 4.6</strong> (via GitHub Copilot in VS Code). The development proceeded in stages, with each stage prompting new questions that led to the next.</p>
<section id="stage-1-hand-scorer" class="level3">
<h3 class="anchored" data-anchor-id="stage-1-hand-scorer">Stage 1: Hand scorer</h3>
<p>The first step was a faithful implementation of the blog’s scoring formula. A 25-card UK Euchre deck (9 through Ace in four suits, plus the Benny/Joker as the highest trump) is dealt, and each hand is scored against every possible trump suit. ANSI colour codes display hearts and diamonds in red, clubs and spades in white, and the Benny in yellow.</p>
</section>
<section id="stage-2-full-hand-simulation" class="level3">
<h3 class="anchored" data-anchor-id="stage-2-full-hand-simulation">Stage 2: Full hand simulation</h3>
<p>A complete four-player simulation engine was built, including:</p>
<ul>
<li><strong>Two-round bidding</strong> — Round 1 considers the turned-up suit; Round 2 considers all other suits. The Opposite Dealer cannot bid in Round 1 (they must pass). If the Benny is turned up, the Dealer is forced to pick it up.</li>
<li><strong>Rule-based play strategy</strong> — players lead trump when calling, lead offsuit when defending, trump in when they can’t follow suit, and apply a “golden rule” to preserve a trump saver for the fifth trick.</li>
<li><strong>UK scoring</strong> — +1 for a regular win (3–4 tricks), +2 for a march (all 5), −2 for getting euchred, all doubled when going alone.</li>
</ul>
</section>
<section id="stage-3-debugging-with-verbose-hand-tracing" class="level3">
<h3 class="anchored" data-anchor-id="stage-3-debugging-with-verbose-hand-tracing">Stage 3: Debugging with verbose hand tracing</h3>
<p>A dedicated <code>debug_hand.py</code> script was created to trace a single hand in full detail: every player’s cards, the scoring calculation against each potential trump, the bid decision versus the threshold, every card played per trick, and the final result. This immediately revealed a strategic bug — the caller was failing to trump in on tricks they needed to win. Two fixes were applied:</p>
<ul>
<li><strong>very_nervous guard</strong> — the logic to hold back trumps was only activated when the player’s partner was already winning the trick, not when opponents were ahead.</li>
<li><strong>desperate mode</strong> — when the caller needed to win all remaining tricks to avoid being euchred, aggressive play was forced.</li>
</ul>
<p>After these fixes, traced hands showed correct play behaviour.</p>
</section>
<section id="stage-4-threshold-sweep-constant-multiplier" class="level3">
<h3 class="anchored" data-anchor-id="stage-4-threshold-sweep-constant-multiplier">Stage 4: Threshold sweep — constant multiplier</h3>
<p>The first optimisation attempt multiplied all bidding thresholds by a single scalar and measured the Expected Points Value (EPV) per played hand. Pre-generated deals with a fixed random seed ensured every multiplier configuration faced identical cards (common random numbers).</p>
<p>This approach had a fundamental flaw: <strong>EPV per played hand always favours extreme conservatism</strong>. A multiplier of 2.0× means players almost never bid, but when they do they almost always win. The metric ignores the cost of not bidding — in a real game, passing lets your opponents bid against you.</p>
</section>
<section id="stage-5-solving-the-staircase-artifact" class="level3">
<h3 class="anchored" data-anchor-id="stage-5-solving-the-staircase-artifact">Stage 5: Solving the staircase artifact</h3>
<p>Even before addressing the strategic flaw, the sweep results showed an unexpected saw-tooth pattern. Adjacent multiplier steps that should produce similar results were jumping erratically.</p>
<p>The cause: the scoring formula produces integer scores, so a threshold of 18.3 behaves identically to 18.0 — both require a score of at least 19. When a multiplier step happens to push several thresholds across integer boundaries simultaneously, EPV jumps; when no thresholds cross, nothing changes.</p>
<p>Two successive fixes were applied:</p>
<ol type="1">
<li><p><strong>Fractional thresholds</strong> — removing the <code>round()</code> call so thresholds like 18.3 are compared directly. This shifted the staircase but didn’t eliminate it, because integer scores still create discrete boundaries.</p></li>
<li><p><strong>Probabilistic thresholds</strong> — at threshold 18.3 with score 18, bid with probability 0.7 (the fractional overlap). This makes EPV a truly continuous function of the multiplier. A seeded RNG ensures reproducibility.</p></li>
</ol>
</section>
<section id="stage-6-monte-carlo-per-player-multipliers" class="level3">
<h3 class="anchored" data-anchor-id="stage-6-monte-carlo-per-player-multipliers">Stage 6: Monte Carlo per-player multipliers</h3>
<p>The breakthrough was changing how opponents are modelled. Instead of all four players using the same multiplier, each player in each deal draws an independent multiplier from a uniform distribution U(0.8, 1.8). After the hand is played, the resulting points are attributed to the bucket matching each player’s multiplier.</p>
<p>This solves the conservatism bias: a player with a high multiplier bids rarely, but their opponents (with potentially lower multipliers) bid against them, scoring points that count as losses for the conservative player. The EPV for each bucket now reflects the <strong>true value of using that strategy in a population of mixed strategies</strong>.</p>
<p>Memory was a concern at 2 million deals. Generating all deals upfront consumed too much RAM, so the simulation was split into multiple runs (e.g.&nbsp;4 × 500,000), with deals generated and freed each run while a single set of accumulators aggregates across all runs.</p>
</section>
</section>
<section id="results" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>The final simulation used 2,000,000 deals (8,000,000 player-observations across 4 runs of 500,000). The EPV curve peaks clearly at <strong>multiplier 1.00</strong> — the blog’s original thresholds:</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2026-03-16-validating-euchre-bidding-thresholds/epv_sweep.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">EPV by bid threshold multiplier with 50%, 90%, and 99% confidence intervals.</figcaption>
</figure>
</div>
<table class="table">
<thead>
<tr class="header">
<th style="text-align: center;">Multiplier</th>
<th style="text-align: center;">EPV</th>
<th style="text-align: center;">±SE</th>
<th style="text-align: center;">Called%</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">0.80</td>
<td style="text-align: center;">−0.0620</td>
<td style="text-align: center;">0.0048</td>
<td style="text-align: center;">45.7%</td>
</tr>
<tr class="even">
<td style="text-align: center;">0.90</td>
<td style="text-align: center;">+0.0225</td>
<td style="text-align: center;">0.0032</td>
<td style="text-align: center;">40.6%</td>
</tr>
<tr class="odd">
<td style="text-align: center;">0.95</td>
<td style="text-align: center;">+0.0465</td>
<td style="text-align: center;">0.0031</td>
<td style="text-align: center;">37.6%</td>
</tr>
<tr class="even">
<td style="text-align: center;"><strong>1.00</strong></td>
<td style="text-align: center;"><strong>+0.0640</strong></td>
<td style="text-align: center;"><strong>0.0031</strong></td>
<td style="text-align: center;"><strong>34.9%</strong></td>
</tr>
<tr class="odd">
<td style="text-align: center;">1.05</td>
<td style="text-align: center;">+0.0584</td>
<td style="text-align: center;">0.0030</td>
<td style="text-align: center;">32.3%</td>
</tr>
<tr class="even">
<td style="text-align: center;">1.10</td>
<td style="text-align: center;">+0.0546</td>
<td style="text-align: center;">0.0030</td>
<td style="text-align: center;">29.8%</td>
</tr>
<tr class="odd">
<td style="text-align: center;">1.20</td>
<td style="text-align: center;">+0.0299</td>
<td style="text-align: center;">0.0029</td>
<td style="text-align: center;">25.1%</td>
</tr>
<tr class="even">
<td style="text-align: center;">1.40</td>
<td style="text-align: center;">−0.0120</td>
<td style="text-align: center;">0.0028</td>
<td style="text-align: center;">17.6%</td>
</tr>
<tr class="odd">
<td style="text-align: center;">1.60</td>
<td style="text-align: center;">−0.0381</td>
<td style="text-align: center;">0.0028</td>
<td style="text-align: center;">11.4%</td>
</tr>
<tr class="even">
<td style="text-align: center;">1.80</td>
<td style="text-align: center;">−0.0676</td>
<td style="text-align: center;">0.0039</td>
<td style="text-align: center;">7.1%</td>
</tr>
</tbody>
</table>
<p>The curve shows the expected trade-off:</p>
<ul>
<li><strong>Below 1.0</strong> (too aggressive): players bid too often on weak hands and get euchred, losing points despite high action.</li>
<li><strong>At 1.0</strong> (the blog’s thresholds): the sweet spot — players bid on roughly 35% of hands with the best risk-reward balance.</li>
<li><strong>Above 1.0</strong> (too conservative): players bid less often and win more when they do, but opponents exploit the passivity. By 1.35 the EPV crosses zero — the conservatism costs more than it saves.</li>
</ul>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The blog’s hand-scoring formula and bidding thresholds are essentially optimal against a mixed-strategy population. The Monte Carlo simulation with 8 million observations confirms that a multiplier of 1.00 produces the highest EPV, with the peak clearly resolved at a standard error of just 0.003 points.</p>
<p>The journey to this result was as instructive as the result itself. Each modelling choice — how to handle integer thresholds, how to model opponents, how to measure success — materially affected the outcome. The constant-multiplier sweep suggested the thresholds should be 50% higher; the Monte Carlo approach showed they were right all along.</p>
</section>
<section id="code" class="level2">
<h2 class="anchored" data-anchor-id="code">Code</h2>
<p>Four Python scripts (no external dependencies beyond matplotlib for plotting):</p>
<ul>
<li><code>euchre.py</code> — deck, scoring formula, threshold constants, coloured card display</li>
<li><code>playhand.py</code> — bidding, play strategy, hand simulation</li>
<li><code>sweep.py</code> — Monte Carlo threshold sweep with batched execution and plotting</li>
<li><code>debug_hand.py</code> — verbose single-hand tracer for strategy validation</li>
</ul>


</section>

 ]]></description>
  <category>statistics</category>
  <category>simulation</category>
  <category>games</category>
  <guid>https://johnhearn.github.io/notes/2026-03-16-validating-euchre-bidding-thresholds/</guid>
  <pubDate>Mon, 16 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>A Measure of Alignment</title>
  <link>https://johnhearn.github.io/notes/2024-08-24-a-measure-of-alignment/</link>
  <description><![CDATA[ 




<p>During the summer holidays I stumbled on a printed copy of my old degree dissertation. It’s over 25 years old and I was pleasantly surprised by the writing style but no so much about the structure - no clear explanation of what was being demonstrated and the conclusions were a bit lacking.</p>
<p>The project itself was to use Monte Carlo simulation techniques to discover things about the phases of idealised liquid crystals, modelled as ellipses and ellipsoids in 2 and 3 dimensions. The original code was written in C (much to my tutors chagrin who wanted me to use FORTRAN).</p>
<p>For some reason, last weekend I though it would be a good idea to rewrite it Julia. So I did and it worked so much better (faster) than back in 1997. Maybe I’ll be able to finish the original aims of the dissertation, 25 years later!</p>

<div class="no-row-height column-margin column-container page-columns page-full"><div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2024-08-24-a-measure-of-alignment/nematic-phase.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">Figure 1: Nematic phase of partially aligned liquid crystals, modelled as ellipses.</figcaption>
</figure>
</div></div><p>One of the properties of liquid crystals is that, at low enough temperatures and high enough pressures, they align with each other in much the same way they do on LCD displays when a voltage is applied. My simulations were able to recreate this effect (see Figure 1). They measured the density of the simulated material, which obviously increases the more the crystals align. I did notice that there wasn’t a direct measure of alignment… so why not invent one?</p>
<p>I needed a formula that I could sum pairwise over all the crystals (ellipses). Ideally the measure should be 0 if all the ellipses are totally unaligned and 1 if totally aligned. Taking two ellipses first, the obvious choice matching these criteria is the cosine of the angle between them, basically cosine similarity. This gives exactly 1 if ellipses are aligned and exactly 0 if they are perpendicular. The ellipses have a symmetry where they should also be measured as aligned if the angle is 180º. I this case the cosine is -1 when it should be 1. A simple way to achieve this is to take either the absolute value of the cosine or its square. This latter option has a nice symmetry to it. Averaging over the <img src="https://latex.codecogs.com/png.latex?n%5E2"> terms we have a first stab at an alignment formula, <img src="https://latex.codecogs.com/png.latex?A">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA%20=%20%5Cfrac%7B1%7D%7Bn%5E2%7D%5Csum_%7Bi,j%20=%201%7D%5En%20cos%5E2(%5Ctheta_i-%5Ctheta_j)%0A"></p>
<p>Where <img src="https://latex.codecogs.com/png.latex?i"> an <img src="https://latex.codecogs.com/png.latex?j"> enumerate over the <img src="https://latex.codecogs.com/png.latex?n"> ellipses to be measured and <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is their respective orientation. To test my formula I generated different configurations and tested their alignment. If all the angles where the same then the alignment did indeed come out as exactly 1. However I realised that there is no way to make more than 2 ellipse totally perpendicular in 2 dimensions. They will always align somewhat with the first or the second. When there are more ellises the situation is worse.</p>

<div class="no-row-height column-margin column-container page-columns page-full"><div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2024-08-24-a-measure-of-alignment/alignment3.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">Figure 2: Minimum alignment for 3 ellipses.</figcaption>
</figure>
</div></div><p>Obviously, the minimum posible alignment of 2 ellipses is when the angle between them is <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Cpi%7D%7B2%7D"> radians, orthogonal. After scratching my head for a while and running different tests, I realised that 3 ellipses is when the angle between them is exactly <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Cpi%7D%7B3%7D"> radians (see Figure 2).</p>
<p>A similar thing happens with larger numbers of particles where the minimum has then dividing the circle equally. This is obviously true for 2 ellipses and I can prove it for 3. I’m taking it to be the case for now.</p>
<p>After a bit of thinking the way to calculate this minimum, <img src="https://latex.codecogs.com/png.latex?M">, is to make the following sum:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AM%20=%20%5Cfrac%7B2%7D%7Bn(n-1)%7D%5Csum_%7Bk=1%7D%5En%20(n-k)cos%5E2(%5Cfrac%7Bk%5Cpi%7D%7Bn%7D)%0A"></p>
<p>Where <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bk%5Cpi%7D%7Bn%7D"> is the angle of the kth ellipse and there are <img src="https://latex.codecogs.com/png.latex?(n-k)"> ellipses with that angular separation. We divide by the number of pairs counted. With a bit of help from the internet this is solvable and has a simple closed form. Using the identity:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Acos%5E2x%20=%20%5Cfrac%7B1+cos%202x%7D%7B2%7D%0A"></p>
<p>and the orthogonality of the cosine funtions we have:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0AM&amp;=%20%5Cfrac%7B2%7D%7Bn(n-1)%7D%5Csum_%7Bk=1%7D%5En%20(n-k)(1+cos%20%5Cfrac%7B2k%5Cpi%7D%7Bn%7D)%20%5C%5C%0A&amp;=%20%5Cfrac%7B2%7D%7Bn(n-1)%7D%5Cleft(%5Csum_%7Bk=1%7D%5En%20(n-k)%20+%20n%5Csum_%7Bk=1%7D%5En%20cos%20%5Cfrac%7B2k%5Cpi%7D%7Bn%7D%20-%20%5Csum_%7Bk=1%7D%5En%20k%20%5Ccdot%20cos%20%5Cfrac%7B2k%5Cpi%7D%7Bn%7D%5Cright)%20%5C%5C%0A&amp;=%20%5Cfrac%7B2%7D%7Bn(n-1)%7D%5Cleft(%5Cfrac%7B1%7D%7B2%7D(n%20-%201)%20n%20-%20n%20-%20%5Cfrac%7B-n%7D%7B2%7D%5Cright)%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7Bn(n-1)%7D%5Cleft(n%5E2-n%20-%202n%20+%20n%5Cright)%20%5C%5C%0A&amp;=%20%5Cfrac%7B1%7D%7B2%7D%5Cleft(%5Cfrac%7Bn-2%7D%7Bn-1%7D%5Cright)%20%5C%5C%0A%5Cend%7Baligned%7D"></p>
<p>Validate: <img src="https://latex.codecogs.com/png.latex?n=2%20%5Cimplies%20M=0">, <img src="https://latex.codecogs.com/png.latex?n=3%20%5Cimplies%20M=%C2%BC">, <img src="https://latex.codecogs.com/png.latex?n=4%20%5Cimplies%20M=%E2%85%93">, etc.</p>
<p>Also: <img src="https://latex.codecogs.com/png.latex?%5Cdisplaystyle%20%5Clim_%7Bn%20%5Crightarrow%20%5Cinfty%7DM=%C2%BD"> as required.</p>
<p>I wanted to factor out this minimum alignment bias from the calculation so that the alignment would always be in the range from 0 to 1. To do that we calculate the proportion between <img src="https://latex.codecogs.com/png.latex?1-M"> and <img src="https://latex.codecogs.com/png.latex?A-M">. So the new formula is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA'%20=%20%5Cfrac%7BA-M%7D%7B1-M%7D%20=%20%5Cfrac%7B2%20A%20(n%20-%201)%20-%20(n%20-%202)%7D%7Bn%7D%0A"></p>
<p>I’ve tested this with different distributions of angles and it gives a reliable indicator of the alignment. The only thing is that the cosine squared makes it quite non-linear having a tendency to be closer to the extremes, 0 and 1. To counteract this an inverse straightens out the results for a smoother gradient, so</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA''%20=%201%20-%5Cfrac%7B2%7D%7B%5Cpi%7Dcos%5E%7B-1%7D%20%5Csqrt%7BA'%7D%0A"></p>
<p>might give better results depending on the application.</p>



 ]]></description>
  <category>mathematics</category>
  <category>physics</category>
  <guid>https://johnhearn.github.io/notes/2024-08-24-a-measure-of-alignment/</guid>
  <pubDate>Sat, 24 Aug 2024 17:30:00 GMT</pubDate>
</item>
<item>
  <title>A Plot Recipe for Yao.jl</title>
  <link>https://johnhearn.github.io/notes/2020-07-26-plot-recipe-for-yao-jl/</link>
  <description><![CDATA[ 




<p>It’s sometimes useful to see a distribution plot for multiple results obtained from a quantum circuit. Take for example the random number generator from a <a href="../2020-07-23-quantum-random-numbers-in-yao-jl/">previous post</a> where we used such a plot to check the results were uniformly distributed. Julia has great support for plotting. With the <a href="https://docs.juliaplots.org/latest/">Plots.jl</a> package it’s extremely easy to get a distribution histogram but getting the axes tidy is a bit fiddly. Fortunately Plots.jl provides a very conveniente extension mechanism which we can use.</p>
<p>As a reminder, here we generate an array of random 3-bit numbers using the <code>nshots</code> parameter to specify how many, in this case 1000.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Yao</span></span>
<span id="cb1-2">n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-3">numbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(n, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(r, nshots<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span></code></pre></div></div>
<pre class="console"><code>1000-element Array{BitBasis.BitStr{3,Int64},1}:
 100 ₍₂₎
       ⋮
 110 ₍₂₎</code></pre>
<p>To see the histogram of results we could do something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">histogram</span>(numbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.|&gt;</span> bint, legend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=:</span>none, xticks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>))</span></code></pre></div></div>
<p>This is OK but it has a few things I don’t like, for one the ticks don’t really line up properly with the bars. Also the ticks have to be manually set. Also the bins often have to be tweaked manually as well. These things are easy to fix and put inside a Plots.jl recipe for easy reuse.</p>
<p>Looking back at the results, Yao.jl gives us an array of <code>BitStr</code> results and the full type <code>Array{BitBasis.BitStr{3,Int64},1}</code> is parametrised by the number of bits. That turns out to be quite useful. Defining the recipe function like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@recipe</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">user_recipe</span>(measurements<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Array{BitStr{n,Int},1}</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span> n</span></code></pre></div></div>
<p>We now have access to the number of bits in <img src="https://latex.codecogs.com/png.latex?n">. The maximum possible value is then <code>max = (1&lt;&lt;n)-1</code>. Then we can calculate the histogram using standard Julia tools:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1">hist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(Histogram, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Int</span>.(measurements), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">hist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize</span>(hist, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=:</span>pdf)</span></code></pre></div></div>
<p>Notice that this is where the conversion to <code>Int</code> happens and we one again use the <code>max</code> value that came via the type parametrisation. Next the plot attributes, basically the same as the histogram used above:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1">seriestype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>bar</span>
<span id="cb6-2">bins <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:=</span> max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-3">xticks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--&gt;</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>max)</span>
<span id="cb6-4">legend <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>none</span></code></pre></div></div>
<p>Again using the <code>max</code> value. Then we can return the histogram results, centering on the tick marks:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1">hist.edges[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, hist.weights</span></code></pre></div></div>
<p>Y voila. Julia will automatically dispatch to our recipe based on the type of the results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(results)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-26-plot-recipe-for-yao-jl/random-dist-8.png" class="img-fluid figure-img" style="width:60.0%"></p>
<figcaption class="margin-caption">Distribution of random numbers produced by the recipe</figcaption>
</figure>
</div>
<p>To check with a different distriibution let’s try imitating a random 6-sided die by preselection of the unwanted values 6 and 7.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">YaoBlocks.ConstGate.P0</span></span>
<span id="cb9-2"></span>
<span id="cb9-3">die <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chain</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, </span>
<span id="cb9-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(H, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb9-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">control</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>X),</span>
<span id="cb9-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>P0))</span>
<span id="cb9-7"></span>
<span id="cb9-8">results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> block <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focus!</span>(r, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(r, nshots<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb9-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(results)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-26-plot-recipe-for-yao-jl/random-dist-6.png" class="img-fluid figure-img" style="width:60.0%"></p>
<figcaption class="margin-caption">Distribution of preselected random numbers produced by the recipe</figcaption>
</figure>
</div>
<p>Distribution shows values 6 and 7 have 0% probability, as we expected.</p>
<p>Working code available <a href="https://nbviewer.jupyter.org/github/johnhearn/notebooks/blob/master/QuantumComputing/A%20Plot%20Recipe%20for%20Yao.jl.ipynb">here</a>.</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>visualisation</category>
  <guid>https://johnhearn.github.io/notes/2020-07-26-plot-recipe-for-yao-jl/</guid>
  <pubDate>Sun, 26 Jul 2020 13:30:00 GMT</pubDate>
</item>
<item>
  <title>The Deutsch-Jozsa Algorithm in Yao.jl</title>
  <link>https://johnhearn.github.io/notes/2020-07-25-deutsch-jozsa-with-yao-jl/</link>
  <description><![CDATA[ 




<p>(Post adapted from a <a href="../2018-10-01-deutsch-jozsa-algorithm/">similar one</a> using <a href="https://yaoquantum.org/">Yao.jl</a> instead of <a href="https://github.com/johnhearn/quko">Quko</a>. <a href="https://qiskit.org/textbook/ch-algorithms/deutsch-josza.html">Here</a> is another one using <a href="https://qiskit.org/">QisKit</a>)</p>
<p>This post describes one of the first quantum algorithms to be discovered that gave a theoretically significant improvement over the classical equivalent. Having said that it’s not useful at all, its main value was that it served as inspiration for other more practical algorithms such as Grover’s search and Shor’s factoring.</p>
<section id="simplest-case" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="simplest-case">Simplest case</h2>
<p>The simplest statement of the problem is to determine if a boolean function, <img src="https://latex.codecogs.com/png.latex?f">, always results in the same <em>constant</em> value or if it is <em>balanced</em> and returns both true and false depending on the input.</p>
<p>In programming terms this means evaluating the function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb1-1">f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Boolean</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Boolean</span></span></code></pre></div></div>
<p>for both possible inputs and checking the results. This amounts to applying an XOR operation to the two results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>)</span></code></pre></div></div>
<p>In the classical world this will obviously require <strong>two</strong> evaluations of the function however in the quantum version only <strong>one</strong> evaluation is ever required.</p>
<p>Imagine we are given a gate <img src="https://latex.codecogs.com/png.latex?U_f"> which is based on the function we are interested in. This gate can be constructed in various ways but for now we’ll assume that it’s given.</p>
<p>Then a circuit for the algorithm can be represented by the following diagram:</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-25-deutsch-jozsa-with-yao-jl/deutschs-circuit.png" class="img-fluid figure-img" width="320"></p>
<figcaption class="margin-caption">Deutsch’s algorithm circuit</figcaption>
</figure>
</div>
<p>This equates in Yao.jl to the following snippet:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutsch</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chain</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb3-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>X),</span>
<span id="cb3-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, H),</span>
<span id="cb3-4">                Uf,</span>
<span id="cb3-5">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>H))</span></code></pre></div></div>
<p>Where <img src="https://latex.codecogs.com/png.latex?U_f"><sup>1</sup> is the block implementing the function. If the function is <em>balanced</em> then the measurement will always be <code>true</code>, if constant then the measurement will always be <code>false</code>.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;One of the things I like about Julia is that it is OK to use naming more akin to the mathematics, even using unicode, than would be conventional in other language conventions.</p></div></div><p>One way to think about how this works is that the <img src="https://latex.codecogs.com/png.latex?U_f"> gate transforms the superposition of both possible input values. The resulting interference pattern provides us with the result.</p>
<p>As an example, if the function, <img src="https://latex.codecogs.com/png.latex?f">, evaluates to a constant value <img src="https://latex.codecogs.com/png.latex?1">. If you work through the logic then the oracle, <img src="https://latex.codecogs.com/png.latex?U_f">, simply flips the second bit. In Yao.jl the block can be created like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1">Uf₁ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>X)</span></code></pre></div></div>
<p>Where <code>put</code> is a Yao.jl function to add an <code>X</code> gate to the second qubit. Then, evaluating the circuit:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutsch</span>(Uf₁) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focus!</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> measure!</span></code></pre></div></div>
<p>Always results in a <code>0</code> to indicate a <em>constant</em> function. On the other hand if we try a balanced function which can be represented by the <code>CNOT</code> gate.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1">Uf₂ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">control</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>X)</span></code></pre></div></div>
<p>In this case evaluating the circuit as before results ins a <code>1</code> indicating that it is balanced.</p>
</section>
<section id="extending-to-multiple-bits" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="extending-to-multiple-bits">Extending to multiple bits</h2>
<p>Other researchers extended to algorithm to multiple bits which corresponds to the function taking an integer argument. The result bust either be <em>constant</em>, as before, or <em>balanced</em>, meaning that the function returns either 1 or 0 half of the time (other possible functions are not contemplated).</p>
<p>The function becomes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb7-1">f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Int</span></span></code></pre></div></div>
<p>and will, on average, need many more evaluations of the function to test be able to determine if it is <em>constant</em> or not.</p>
<p>The quantum circuit in this case would be:</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-25-deutsch-jozsa-with-yao-jl/deutsch-jozsa-circuit.png" class="img-fluid figure-img" width="400"></p>
<figcaption class="margin-caption">Deutsch-Jozsa algorithm circuit</figcaption>
</figure>
</div>
<p>One way to build this circuit in Yao.jl is as follows:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutsch_jozsa</span>(m, Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chain</span>(m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb8-2">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span>X),</span>
<span id="cb8-3">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, H),</span>
<span id="cb8-4">                        Uf,</span>
<span id="cb8-5">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(H, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>m))</span></code></pre></div></div>
<p>In this case <img src="https://latex.codecogs.com/png.latex?m"> is the number of bits in the integer part of the circuit. This will give us a boolean result for the given function, assuming the function works on <img src="https://latex.codecogs.com/png.latex?m"> bit integers. To build the oracle, <img src="https://latex.codecogs.com/png.latex?U_f">, we’ll use some bit fiddling to create a permutation:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1">perm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x for y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> for x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div></div>
<p>The double comprehension syntax is very convenient here. Compare the calculation <code>y ⊻ f(x)</code> (where ⊻ means XOR) with the <a href="https://en.wikipedia.org/wiki/Deutsch%E2%80%93Jozsa_algorithm#Algorithm">definition</a> of the gate.</p>
<p>To convert this to a Yao.jl block we have to create a permutation matrix (complex for generality) and then convert to a matrix block <code>matblock</code> for Yao.jl to understand it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">permute</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparse</span>(I, N, N), perm<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-2">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Matrix</span>{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Complex</span>{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Float64</span>}} <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">     matblock</span></code></pre></div></div>
<p>We can then test our circuit with a function, say <img src="https://latex.codecogs.com/png.latex?f(x)=1"> which should give us <img src="https://latex.codecogs.com/png.latex?0"> since it’s constant:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># constant</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutsch_jozsa</span>(m, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Uf</span>(m, f)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">focus!</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>m) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> measure!</span></code></pre></div></div>
<p>Which indeed results in 0.</p>
<p>To see this all working take a look at <a href="https://nbviewer.jupyter.org/github/johnhearn/notebooks/blob/master/QuantumComputing/The%20Deutsch-Jozsa%20Algorithm%20in%20Yao.jl.ipynb">this notebook</a>.</p>


</section>


 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2020-07-25-deutsch-jozsa-with-yao-jl/</guid>
  <pubDate>Sat, 25 Jul 2020 12:26:00 GMT</pubDate>
</item>
<item>
  <title>Grover’s Algorithm in Yao.jl</title>
  <link>https://johnhearn.github.io/notes/2020-07-25-grovers-with-yao-jl/</link>
  <description><![CDATA[ 




<div class="page-columns page-full"><p>Continuing the series of posts building the basic quantum algorithms with <a href="https://github.com/QuantumBFS">Yao.jl</a> we come to Grover’s algorithm. There is a <a href="https://tutorials.yaoquantum.org/dev/generated/quick-start/3.grover-search/">tutorial on Grover</a> in the Yao.jl documentation  but I found it a little hard to follow the code so I simplified it right down to the basics.</p><div class="no-row-height column-margin column-container"><span class="">QisKit also has a <a href="https://qiskit.org/textbook/ch-algorithms/grover.html">nice explication</a>. It’s worth getting multiple perspectives on these things.</span></div></div>
<p><a href="../2018-10-03-grovers-search/">Remember</a> that we have a binary function, <img src="https://latex.codecogs.com/png.latex?f">, which is always equal <img src="https://latex.codecogs.com/png.latex?0"> except for a single value <img src="https://latex.codecogs.com/png.latex?u">, when it is equal to <img src="https://latex.codecogs.com/png.latex?1">. The challenge is to find <img src="https://latex.codecogs.com/png.latex?u"> with as few queries to <img src="https://latex.codecogs.com/png.latex?f"> as possible. As always, and with all the usual caveats, we take for granted that we have access to an oracle, <img src="https://latex.codecogs.com/png.latex?U_f">, that provides a suitable quantum transformation based on <img src="https://latex.codecogs.com/png.latex?f">.</p>
<p>The oracle consists of a transformation which reflects the target value, <img src="https://latex.codecogs.com/png.latex?u">, and only this value, around the x-axis. All other values are left untouched.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(u<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">T</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span> T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Unsigned</span></span>
<span id="cb1-2">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ceil</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, u)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use only as many bits as necessary</span></span>
<span id="cb1-3">    v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ones</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ComplexF64</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>n)</span>
<span id="cb1-4">    v[u<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Flip the value we're looking for</span></span>
<span id="cb1-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Diagonal</span>(v)</span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>We work out the smallest number of bits needed from the value itself. In the Yao.jl tutorial this is all on one line and quite cryptic, hopefully this version is easier to read.</p>
<div class="page-columns page-full"><p>In the tutorial they use the phased version of the diffusion operator:</p><div class="no-row-height column-margin column-container"><span class="">This diagram is generated directly from the code with YaoDraw :)</span></div></div>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-25-grovers-with-yao-jl/grovers-circuit-phase.png" class="img-fluid figure-img" width="400"></p>
<figcaption class="margin-caption">grovers-circuit</figcaption>
</figure>
</div>
<p>Notice that with this construction we don’t have to use an ancillary bit. As can be seen in the diagram we also have some reusable blocks, namely the <img src="https://latex.codecogs.com/png.latex?H%5E%7B%5Cotimes%20n%7D">, which for some reason the tutorial calls <code>gen</code>, and the repeating section which is a combination of the oracle itself followed by the diffusion operator. They are chained together like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1">    gen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(n, H)</span>
<span id="cb2-2">    reflect0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">control</span>(n, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">-collect</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;-</span>Z) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I-2|0&gt;&lt;0|</span></span>
<span id="cb2-3">    repeating_circuit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chain</span>(Uf, gen, reflect0, gen)</span></code></pre></div></div>
<p>Inside the repeating section there is also the reflection circuit, <code>reflect0</code> which is responsible for flipping the distribution about the average value which is what has the effect of amplifying values made negative by the oracle. To check that it is indeed the correct circuit it can be compared to the other form <img src="https://latex.codecogs.com/png.latex?%20I_n%20-%202%5Cvert%200%5En%20%5Crangle%20%5Clangle%200%5En%20%5Cvert%20">.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ZERO</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], n))</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Int</span>.(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">real</span>.(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparse</span>(I, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">*ZERO</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">*ZERO</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'))</span></span></code></pre></div></div>
<p>It is indeed the same, although I’m still sure of the origin of the conditional <code>Z</code> transform version, it’s an identity to bear in mind.</p>
<p>Continuing with the algorithm, the repeating section is simply placed in a for loop applied to a prepared quantum register:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1">    reg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> gen</span>
<span id="cb4-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>iter</span>
<span id="cb4-3">        reg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> repeating_circuit</span>
<span id="cb4-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<div class="page-columns page-full"><p>The variable <code>iter</code> is the number of iterations we want to apply. Getting the right value for this is tricky so we just use a hand picked value for now. It should be less than <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B2%5En%7D">. For clarity we wrap the whole circuit in a function and pass in the oracle:</p><div class="no-row-height column-margin column-container"><span class="">The tutorial has much more sophisticated way of doing this.</span></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grovers</span>(Uf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">AbstractBlock{n}</span>, iter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">where</span> n</span>
<span id="cb5-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grovers</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matblock</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0b11110011</span>)), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> measure!</span></code></pre></div></div>
<p>And that’s it, not too bad actually. Working code is <a href="https://nbviewer.jupyter.org/github/johnhearn/notebooks/blob/master/QuantumComputing/Grover%27s%20Algorithm%20in%20Yao.jl.ipynb">here</a>.</p>
<p>Looking forward to applying to more <a href="https://www.youtube.com/watch?v=afuoGbptET8">interesting problems</a> to it.</p>
<hr>
<p>PS: Just for fun this is how the histogram of probabilities evolves between each iteration. It’s clear how the chosen value emerges in just a few steps.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-25-grovers-with-yao-jl/grovers-search-anim.gif" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">grovers-search</figcaption>
</figure>
</div>
<p>If you’re interested, this is the code to generate the histogram. Run with 6 bits over 6 iterations and 1000 shots at each iteration to get the probability.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Plots</span></span>
<span id="cb6-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb6-3">  anim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@animate</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>iter</span>
<span id="cb6-4">    numbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> reg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(r, nshots<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.|&gt;</span> bint</span>
<span id="cb6-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">histogram</span>(numbers, normed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>, legend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=:</span>none, xlims<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">63</span>), ylims<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), nbins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>)</span>
<span id="cb6-6">    reg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> repeating_circuit</span>
<span id="cb6-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb6-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gif</span>(anim, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grovers-search.gif"</span>, fps <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2020-07-25-grovers-with-yao-jl/</guid>
  <pubDate>Sat, 25 Jul 2020 12:26:00 GMT</pubDate>
</item>
<item>
  <title>Quantum Random Numbers in Yao.jl</title>
  <link>https://johnhearn.github.io/notes/2020-07-23-quantum-random-numbers-in-yao-jl/</link>
  <description><![CDATA[ 




<p>The first example I use for understanding quantum computing concepts is the <a href="../2018-09-30-quantum-random-number-generator/">Quantum Random Number Generator</a>. It’s also the simplest example possible. Let’s see what it looks like <a href="https://github.com/QuantumBFS">Yao.jl</a>.</p>
<p>As you might expect, it’s beyond trivial.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Yao</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">BitBasis</span></span>
<span id="cb1-2">n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(n, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> measure! <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> bint</span></code></pre></div></div>
<p>Here <code>Yao</code> is the base package. <code>BitBasis</code> contains the tools for dealing with bit strings which Yao returns.</p>
<p>We pass a <code>zero_state</code> with <code>n</code> qubits, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?%5Cvert%200%5En%20%5Crangle">, through <code>n</code> hadamard gates and then measure the result. Each bit has a 50/50 chance of being 0 or 1. In this case we use the <code>bint</code> function from the <code>BitBasis</code> package to convert the measured bits to a number.</p>
<p>The result is indeed a random number between 0 and 7.</p>
<p>Let’s go a little further and make sure the numbers are uniform by plotting the distribution of the results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1">results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">zero_state</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeat</span>(n, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(r, nshots<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.|&gt;</span> bint</span></code></pre></div></div>
<p>The only difference here is the <code>nshots</code> parameter passed to the measure function to repeat the measurement that many times. This is much faster and cleaner than using a comprehension of something. the <code>.|&gt;</code> operator converts the individual results.</p>
<p>The result is an array of 10,000 measurements which should be uniformly distributed. Let’s check:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">histogram</span>(results, legend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=:</span>none, xticks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">maximum</span>(results)), bar_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span></code></pre></div></div>
<p>Gives us</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2020-07-23-quantum-random-numbers-in-yao-jl/uniform-distribution.png" class="img-fluid figure-img" width="200"></p>
<figcaption class="margin-caption">Uniformly distributed random numbers.</figcaption>
</figure>
</div>
<p>Which is good.</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2020-07-23-quantum-random-numbers-in-yao-jl/</guid>
  <pubDate>Thu, 23 Jul 2020 19:42:00 GMT</pubDate>
</item>
<item>
  <title>Designing a Talk</title>
  <link>https://johnhearn.github.io/notes/2019-09-14-designing-a-talk/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>Edit: I did the talk laid out in this post on the 10th of October 2019. It mostly followed the outline presented here but in the heat of the moment I accidentally skipped the ice-breaker and much of the introduction, I guess because of nerves. Of the feedback that I got, one person felt that it had lacked a better narrative structure, which is exactly what I did try to do, unsuccessfully it seems. Next talk will be about Conway’s Law at the “Papers We Love” Meetup group. Let’s see how that goes.</p>
</blockquote>
<hr class="slender" width="50%">
<p>One of the nicest things about working at Codurance<sup>1</sup> is the bi-weekly <em>catch-ups</em>. Those of us who are working mainly at client offices go to the Codurance office, meet other Codurancers who we haven’t seen for a while, eat together and share our experiences. We also do lightning talks about things that interest us and at the same time get a chance to practice our presentation skills. After doing many of these internal talks, someone said in a peer review that maybe I should take it up a notch and do a talk externally. Always trying to push out of my comfort zone (aka a sucker for punishment) I proposed a talk at the Barcelona Software Crafters conference, with a very short synopsis.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Edit: This was written before I left Codurance for pastures new. The content of the post regarding my time there still stands.</p></div></div><blockquote class="blockquote">
<p>How predictable is your code? How predictable is your system, your project or your business? What does it mean to be predictable? Knowing this will help us write better code, create better systems and run better projects and help us understand why agile practices work and when they may not be enough…</p>
</blockquote>
<p>To my astonishment it was accepted.</p>
<p>The theme of the talk was clear to me and I had (in my head) a clear narrative to explain.</p>
<ul>
<li>Predictably unpredictable code (traditional complexity metrics)</li>
<li>Predictably unpredictable systems (Game of Life, deterministic complexity)</li>
<li>Predictably unpredictable complex adaptive systems (human systems, Systems Thinking and Cynefin)</li>
</ul>
<p>However, as I tried to nail down the actual slides I realised it wouldn’t be as easy as I had anticipated. For one, as I added individual ideas which I wanted to cover I couldn’t find a clean, linear arc for the presentation (things seem much cleaner in one’s head). What’s more, during the research, more and more related topics came up and I couldn’t cover them all. And all the while I still had to be true to the original proposal.</p>
<p>So I wrote down a new possible arc:</p>
<ul>
<li>Connection</li>
<li>Interaction</li>
<li>Systems/Adaptation</li>
</ul>
<p>With this outline I had the same problem, I had simply pivoted what was essentially a two-dimensional idea:</p>
<table class="table">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th>Code</th>
<th>System</th>
<th>CAS</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>Connection</strong></td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>Interaction</strong></td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>Adaptation</strong></td>
<td></td>
<td></td>
<td>x</td>
</tr>
</tbody>
</table>
<p>So I can traverse this row-wise or column-wise, top-down or bottom-up, right to left or left to right. I chose column-wise, left-right, top-down.</p>
<ul>
<li>Code - Connection (traditional complexity)</li>
<li>System - Connection - Interaction (dynamic complexity, Systems Thinking)</li>
<li>CAS - Connection - Interaction - Adaptation (adaptive complexity, Cynefin)</li>
</ul>
<p>Wrapping in an introduction and a final section which rewinds the talk, I have the high-level, linear arc I needed.</p>
<ul>
<li><p>Ice breaker (local feedback demo with clapping)</p></li>
<li><p>Introduction</p>
<ul>
<li>drop pen</li>
<li>if only we could predict the future (BTTF II)</li>
<li>tireless search for predictability doesn’t always work</li>
<li>raising the bar (Alex Bolboaca)</li>
<li>recipes vs principles (Heston Blumenthal)</li>
</ul></li>
<li><p>Connections in code</p>
<ul>
<li>condition separation (coupling)</li>
<li>gilded rose</li>
<li>duplicated code</li>
<li><strong>first definition of complexity</strong></li>
<li>common in TDDed code</li>
<li>importance of refactoring to reduce implicit connections</li>
<li>doing things more than once</li>
</ul></li>
<li><p>Connections in systems</p>
<ul>
<li>system connections and coupling; <a href="https://www.youtube.com/watch?v=ARkLVvtxUZI">a system is not a tree</a> (Henney)</li>
<li>indirect coupling</li>
<li>Relationship between things as well as the things themselves (Mary Poppendick (<a href="http://www.leanessays.com/2019/07/grown-up-lean.html">here</a>) (The dance as opposed to the dancers)</li>
<li>“dynamic” (Scorpios quote) (monitoring)</li>
</ul></li>
<li><p>Interactions in systems</p>
<ul>
<li>self-reinforcing coupling (logging CPU -&gt; scaling -&gt; problems -&gt; logging CPU)</li>
<li>Netflix scale (vizceral screenshot)</li>
<li><strong>second definition of complexity</strong></li>
<li>many failures identified only in retrospect</li>
<li>resilience (modularity, redundancy, diversity, anti-fragile)</li>
<li>dangers of interfering with such a system (mosquitos, unexpected consequences, cobra effect)</li>
<li>Systems Thinking (outside looking in)</li>
</ul>
<hr></li>
<li><p>More interactions in systems</p>
<ul>
<li>pushing complexity: Game of Life and friends</li>
<li><strong>third definition of complexity</strong></li>
<li>emergence; phases</li>
<li>in nature (you; weather, on Jupiter, sun spots, three-body problem, galactic filaments; hexagonal columns, traffic jams, washboarding, ripples, petri dishes, gravity or even time itself)</li>
<li>The End of Certainty</li>
</ul></li>
<li><p>Connections in CASs</p>
<ul>
<li><p>crowd dynamics (eg. Mecca: “simulation is part but not enough”)</p></li>
<li><p>social systems are another level (families, communities, social networks, cities, countries) -The realm of policy and politics</p></li>
<li><p>adaptive, independent agents - inside looking out</p></li>
<li><p><strong>fourth definition of complexity</strong></p></li>
<li><p>predictable irrationality (confirmation bias, success bias, etc.) (computer learns to predict unpredictability)</p></li>
<li><p>Self-organising (back to <strong>clapping</strong>)</p></li>
<li><p>Cynefin; decision landscape (mapping), catastrophic fold, disorder domain; pre-scrum</p></li>
<li><p>agile in complicated or simple domain is wasteful; safe-to-plan</p></li>
<li><p>predicting things in the complex domain is futile. <strong>Design for serendipity</strong>, deliberate connections, coherent probes and experiments; safe-to-fail</p></li>
<li><p>Link with OODA, 3X</p></li>
<li><p>Dancing with systems; future of agility</p></li>
</ul></li>
<li><p>Summary</p>
<ul>
<li>Rewind back through different types of complexity</li>
</ul></li>
<li><p>Questions</p></li>
</ul>




 ]]></description>
  <category>communication</category>
  <guid>https://johnhearn.github.io/notes/2019-09-14-designing-a-talk/</guid>
  <pubDate>Sat, 14 Sep 2019 11:30:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 7</title>
  <link>https://johnhearn.github.io/notes/2019-07-26-building-a-qpu-simulator-in-julia-part-7/</link>
  <description><![CDATA[ 




<p>Unlike Deutsch’s algorithm, which merely shows the possibility of quantum speed-up but isn’t of any practical use, <a href="https://quantum-algorithms.herokuapp.com/299/paper/node1.html">Shor’s algorithm</a> has considerable practical applications. Indeed it excites many people because it could potentially be used to break much of the secure online communications which are used today.</p>
<p>Using tools from number theory, the algorithm consists of a procedure which transforms the problem of prime factorings into a problem of period finding. The advantage of this is that the period finding part, which is exponentially hard on classical computers, can be implemented efficiently (i.e.&nbsp;in polynomial time) with a quantum computer.</p>
<p>The details of the algorithm can be found across the internet. Here’s a <a href="https://www.youtube.com/watch?v=8EcLYB6VD4s">video of a talk</a> by James Birnie but I recommend you to also look for your own research material. There’s some non-trivial maths going on and it’s not easy to understand all of the different elements involved in the algorithm. It helps to read multiple different tutorials and explanations.</p>
<p>Anyway, for the moment, let’s just implement the classical parts in Julia. It turns out to be quite simple. These are the steps:</p>
<ul>
<li>Step 1: choose a random number 1 &lt; a &lt; N</li>
<li>Step 2: find r, the period of <img src="https://latex.codecogs.com/png.latex?a%5Ex%20(mod%20N)"> with respect to x</li>
<li>Step 3: check that r is even and <img src="https://latex.codecogs.com/png.latex?a%5E%7Br/2%7D+1%20%5Cneq%200%20(mod%20N)"></li>
<li>Step 4: calculate the factors (p,q) as <img src="https://latex.codecogs.com/png.latex?p%20=%20gcd(a%5E%7Br/2%7D-1,%20N)"> <img src="https://latex.codecogs.com/png.latex?q%20=%20gcd(a%5E%7Br/2%7D+1,%20N)"></li>
</ul>
<p>Obviously before we can do anything we need a number to factor. Let’s use 37 * 41 = 1517.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number to factor, a multiple of two prime factors &gt; 2</span></span>
<span id="cb1-2">N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1517</span></span></code></pre></div></div>
<p>The first step is to choose a random number <img src="https://latex.codecogs.com/png.latex?a%20%3C%20N">. Also we don’t want to use the degenerate case of <img src="https://latex.codecogs.com/png.latex?1">.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BigInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N))</span></code></pre></div></div>
<p>We use Julia’s arbitrary precision integers (<code>BigInt</code>s) because the exponents get very big very quickly and even 64 bit integers will likely overflow.</p>
<p>The next step is to find the period, say <img src="https://latex.codecogs.com/png.latex?r">, of <img src="https://latex.codecogs.com/png.latex?a%5Ex%20mod%20N"> with respect to <img src="https://latex.codecogs.com/png.latex?x">. This is the operation that will be done with a quantum computation. For now we’ll do it with a brute force approach but remember that this would be prohibitively time-consuming when the number to factor is large.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> N</span>
<span id="cb3-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">global</span> r</span>
<span id="cb3-4">    r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>After running the above code let’s look at the results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@show</span> N, a, r</span>
<span id="cb4-2">(N, a, r) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1517</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1371</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>)</span></code></pre></div></div>
<p>So we have our period, 180. Lucky we used arbitrary precision integers, <img src="https://latex.codecogs.com/png.latex?1371%5E%7B180%7D"> is a very, very big number!</p>
<p>Next, for the calculation to work we need to check that the period is even and that: <img src="https://latex.codecogs.com/png.latex?a%5E%7Br/2%7D+1%20%5Cneq%200%20(mod%20N)">. If it isn’t then we just try again with another value for <img src="https://latex.codecogs.com/png.latex?a">.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb5-2">   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb5-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>Finally we calculate the factors, <img src="https://latex.codecogs.com/png.latex?p"> and <img src="https://latex.codecogs.com/png.latex?q">. Julia gives us an implementation of the greatest common divisor algorithm as part of its base functions, so no need to worry about that. The factors are <img src="https://latex.codecogs.com/png.latex?p=gcd(a%5E%7Br/2%7D-1,%20N)"> and <img src="https://latex.codecogs.com/png.latex?q=gcd(a%5E%7Br/2%7D+1,%20N)">. To keep the types as integers we can use the bit shift operator rather then dividing by 2. In Julia this is simply:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1">    p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcd</span>(a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N)</span>
<span id="cb6-2">    q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcd</span>(a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N)</span></code></pre></div></div>
<p>So what’s the result?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@show</span> p, q</span>
<span id="cb7-2">(p, q) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">37</span>)</span></code></pre></div></div>
<p>Which are the numbers we first thought of!</p>
<p>Right, that’s the easy part and with Julia it was very straightforward due to the native arbitrary precision integers and exponent operators. The hard part is the actual quantum algorithm which we’ll come back to in a later post.</p>
<p>So here’s the classical algorithm ion it’s entirety. There are some edge case that are not being checked but this is basically it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Number to factor, a multiple of two prime factors &gt; 2</span></span>
<span id="cb8-2">N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1517</span></span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: choose a random number 1 &lt; a &lt; N</span></span>
<span id="cb8-5">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BigInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N))</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: find r, the period of a^x mod N</span></span>
<span id="cb8-8">r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> N</span>
<span id="cb8-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">global</span> r</span>
<span id="cb8-12">    r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb8-14"></span>
<span id="cb8-15"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@show</span> N, a, r</span>
<span id="cb8-16"></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: check that r is even and a^(r/2)+1 != 0 mod N</span></span>
<span id="cb8-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb8-19"></span>
<span id="cb8-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: calculate the factors</span></span>
<span id="cb8-21">    p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcd</span>(a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N)</span>
<span id="cb8-22">    q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcd</span>(a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N)</span>
<span id="cb8-23"></span>
<span id="cb8-24">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@show</span> p, q</span>
<span id="cb8-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>mathematics</category>
  <guid>https://johnhearn.github.io/notes/2019-07-26-building-a-qpu-simulator-in-julia-part-7/</guid>
  <pubDate>Fri, 26 Jul 2019 09:50:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 6</title>
  <link>https://johnhearn.github.io/notes/2019-07-25-building-a-qpu-simulator-in-julia-part-6/</link>
  <description><![CDATA[ 




<p>The <a href="https://en.wikipedia.org/wiki/Quantum_Fourier_transform">Quantum Fourier Transform</a> (QFT) is an important quantum operation and an essential part of the period finding step needed for <a href="https://quantum-algorithms.herokuapp.com/299/paper/node1.html">Shor’s algorithm</a>. It can be built as a quantum circuit but it’s very easy to build its unitary matrix directly, the details are in the Wikipedia article. In Julia, this was even easier than I expected. Once we have calculated a couple of constants, we can use a 2-d <code>for</code> comprehension to create the individual elements of the array in a single line.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qft</span>(n)</span>
<span id="cb1-2">   N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>n</span>
<span id="cb1-3">   ω <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">π</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N)</span>
<span id="cb1-4">   a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(N)</span>
<span id="cb1-5">   [a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ω<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>j) for i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>Here <img src="https://latex.codecogs.com/png.latex?n"> is the number of bits and <img src="https://latex.codecogs.com/png.latex?N"> the size of the matrix, which as we know grows exponentially with the number of bits. <img src="https://latex.codecogs.com/png.latex?%CF%89"> is the first complex <img src="https://latex.codecogs.com/png.latex?N%5E%7Bth%7D"> root of unity and <img src="https://latex.codecogs.com/png.latex?a"> the normalising factor. The comprehension runs over both axis <img src="https://latex.codecogs.com/png.latex?i"> and <img src="https://latex.codecogs.com/png.latex?j"> computing the element at each position.</p>
<p>We can test this against the example from the Wikipedia article:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@test</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qft</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">≈</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>; <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span>; <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>; <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span>]</span></code></pre></div></div>
<p>In the above it’s going to generate complex numbers with Float64 precision. For our quantum gates this level of precision is not necessary and uses more memory. A few tweaks to the function will use Float32 instead.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qft</span>(n)</span>
<span id="cb3-2">   N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>n</span>
<span id="cb3-3">   ω <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2f0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">π</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">im</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N)</span>
<span id="cb3-4">   a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1f0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Float32</span>(N))</span>
<span id="cb3-5">   [a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ω<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>j) for i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>And test that it does indeed generate the correct type.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@test</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">typeof</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qft</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Array</span>{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Complex</span>{<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Float32</span>},<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}</span></code></pre></div></div>
<p>What about the performance? Let’s generate the QFT for different numbers of bits.</p>
<pre class="console"><code>&gt; @time qft(11)
0.384501 seconds (6 allocations: 32.000 MiB, 11.74% gc time)
2048×2048 Array{Complex{Float32},2}

&gt; @time qft(12)
1.469458 seconds (6 allocations: 128.000 MiB, 3.34% gc time)
4096×4096 Array{Complex{Float32},2}

&gt; @time qft(13)
6.037579 seconds (6 allocations: 512.000 MiB, 0.74% gc time)
8192×8192 Array{Complex{Float32},2}

&gt; @time qft(14)
25.163830 seconds (6 allocations: 2.000 GiB, 0.20% gc time)
16384×16384 Array{Complex{Float32},2}</code></pre>
<p>The exponential increase in time and space is evident and we quickly reach practical limits. The array is dense because the values are always non-zero but since the calculation time is relatively small there could be considerable advantage using a <a href="https://github.com/traktofon/ComputedArrays.jl"><code>ComputedArray</code></a> instead of a normal array in this case.</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>mathematics</category>
  <guid>https://johnhearn.github.io/notes/2019-07-25-building-a-qpu-simulator-in-julia-part-6/</guid>
  <pubDate>Thu, 25 Jul 2019 18:50:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 4</title>
  <link>https://johnhearn.github.io/notes/2019-06-17-building-a-qpu-simulator-in-julia-part-4/</link>
  <description><![CDATA[ 




<p>Recall that <a href="../2018-10-01-deutsch-jozsa-algorithm/">Deutsch’s algorithm</a> can be represented by the following circuit:</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2019-06-17-building-a-qpu-simulator-in-julia-part-4/deutschs-circuit.png" class="img-fluid figure-img" width="320"></p>
<figcaption class="margin-caption">Deutsch’s algorithm circuit</figcaption>
</figure>
</div>
<p>We want something like this, remembering that Julia is 1-index based:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutch</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(ZERO, ONE) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, eye) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<p>We already have most of the machinery to make this work. We need an overloaded <code>register</code> function to create a register with the given states. This is straightforward and very similar to the <code>gate</code> function we defined in the last post.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">...</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, x)</span></code></pre></div></div>
<p>Next a <em>pipeable</em> <code>measure</code> function that takes the bit <code>k</code> that we want to measure (1-indexed).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(n, k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure!</span>(n, k, qubits)[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>][k]</span></code></pre></div></div>
<p>To avoid having to pass in the size of the register, we can calculate it from the length of the <code>qubits</code> vector. We know that the length, <img src="https://latex.codecogs.com/png.latex?l">, of the vector is <img src="https://latex.codecogs.com/png.latex?l=2%5E%7Bn%7D"> so <img src="https://latex.codecogs.com/png.latex?n=%5Clfloor%20log_%7B2%7D(l)%20%5Crfloor">. In code that becomes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Int</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log2</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(qubits))))</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure!</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(qubits), k, qubits)[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>][k]</span></code></pre></div></div>
<p>All that remains is to define the oracle <img src="https://latex.codecogs.com/png.latex?U_%7Bf%7D">. For 2-bit inputs there are four possible functions:</p>
<div class="line-block"><img src="https://latex.codecogs.com/png.latex?x"> || <img src="https://latex.codecogs.com/png.latex?f_1(x)=0"> | <img src="https://latex.codecogs.com/png.latex?f_2(x)=x"> | <img src="https://latex.codecogs.com/png.latex?f_3(x)=%5Cbar%7Bx%7D"> | <img src="https://latex.codecogs.com/png.latex?f_4(x)=1"> |</div>
<p>|:-:++:—–:+:—–:|:—–:|:—–:| | 0 || 0 | 0 | 1 | 1 | | 1 || 0 | 1 | 0 | 1 | |||constant|balanced|balanced|constant|</p>
<p>The transformation we need for the oracle is from <img src="https://latex.codecogs.com/png.latex?%5Cket%7Bx%7D%5Cket%7By%7D"> to <img src="https://latex.codecogs.com/png.latex?%5Cket%7Bx%7D%5Cket%7By%E2%8A%95f(x)%7D"> which we can write out explicitly.</p>
<table class="table">
<thead>
<tr class="header">
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?x"> <img src="https://latex.codecogs.com/png.latex?y"></th>
<th></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_1(x)"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_2(x)"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_3(x)"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_4(x)"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">0 0</td>
<td></td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
</tr>
<tr class="even">
<td style="text-align: center;">0 1</td>
<td></td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
</tr>
<tr class="odd">
<td style="text-align: center;">1 0</td>
<td></td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
</tr>
<tr class="even">
<td style="text-align: center;">1 1</td>
<td></td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
</tr>
</tbody>
</table>
<p>Reading off the columns we have:</p>
<p>|:—:|:-|:—–: | <img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_1(x)"> | <code>y</code> | I | <img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_2(x)"> | <code>x?~y:y</code> | Controlled NOT | <img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_3(x)"> | <code>y?~x:x</code> | Reversed Controlled NOT | <img src="https://latex.codecogs.com/png.latex?y%E2%8A%95f_4(x)"> | <code>~y</code> | NOT</p>
<p>The matrices that do those transformations can be written as <a href="https://en.wikipedia.org/wiki/Matrix_addition#Direct_sum">direct sums</a>,which is available in Julia as the <a href="https://docs.julialang.org/en/v1/stdlib/SparseArrays/index.html#SparseArrays.blockdiag"><code>blockdiag</code></a> function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1">Uf1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blockdiag</span>(eye, eye) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I</span></span>
<span id="cb5-2">Uf2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blockdiag</span>(eye, NOT) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># C-NOT</span></span>
<span id="cb5-3">Uf3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blockdiag</span>(NOT, eye) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reversed C-NOT</span></span>
<span id="cb5-4">Uf4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">blockdiag</span>(NOT, NOT) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NOT</span></span></code></pre></div></div>
<p>If the function, <img src="https://latex.codecogs.com/png.latex?f">, is <em>balanced</em> then the measurement will always be true, if <em>constant</em> then the measurement will always be false. Let’s evaluate them all together:</p>
<pre class="console"><code>julia&gt; map(deutch, [Uf1, Uf2, Uf3, Uf4])
Bool[false, true, true, false]</code></pre>
<p>We can see that this is exactly the result we expect.</p>
<p>Extending to multiple bits, the generalised quantum circuit in this case is:</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2019-06-17-building-a-qpu-simulator-in-julia-part-4/deutsch-jozsa-circuit.png" class="img-fluid figure-img" width="400"></p>
<figcaption class="margin-caption">Deutsch-Jozsa algorithm circuit</figcaption>
</figure>
</div>
<p>This is known as the Deutsch-Jozsa algorithm. The wikipedia article has much more information about how this works. In our Julia DSL we can easily extend to 2-bit functions:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutchJosza</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(ZERO, ZERO, ONE) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, H, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, H, eye) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-5">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<p>We could determine the transformations <img src="https://latex.codecogs.com/png.latex?U_f"> like before by writing them out explicitly but new we have 8 possible permutations. Rather than writing them out let’s write a function to do it for us:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span>, f) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">begin</span></span>
<span id="cb8-2">    perm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Dict</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{Int,Int}</span>()</span>
<span id="cb8-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-5">            xy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span> n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># |x&gt;|y&gt;</span></span>
<span id="cb8-6">            fxy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># |y⊕f(x)&gt;</span></span>
<span id="cb8-7">            xfxy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span> n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> fxy  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># |x&gt;|y⊕f(x)&gt;</span></span>
<span id="cb8-8">            perm[xy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> xfxy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mapping (1-index based)</span></span>
<span id="cb8-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb8-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb8-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">permmat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>((it) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> perm[it], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb8-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb8-13"></span>
<span id="cb8-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">permmat</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">π</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparse</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">π</span>), <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">π</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
<p>This function goes through the <img src="https://latex.codecogs.com/png.latex?2%5En"> possible values of <img src="https://latex.codecogs.com/png.latex?%5Cket%7Bx%7D"> and the two possible values of <img src="https://latex.codecogs.com/png.latex?%5Cket%7By%7D%20%5Cin%20(0,1)"> and builds a transformation table, <code>perm</code>, of the values <img src="https://latex.codecogs.com/png.latex?%5Cket%7Bx%7D%5Cket%7By%7D"> to <img src="https://latex.codecogs.com/png.latex?%5Cket%7Bx%7D%5Cket%7By%E2%8A%95f(x)%7D">. This table is then used to create a (sparse) permutation matrix for the transformation, e.g.&nbsp;<img src="https://latex.codecogs.com/png.latex?U_f">. This is a very brutish way of doing it but it gets the job done.</p>
<p>Finally we can run the algorithm and check it works</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deutchJosza</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(ZERO, ZERO, ONE) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, H, H) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(Uf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gate</span>(H, H, eye) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-5">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-6">                (y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reinterpret</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span>, y.chunks)[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb9-7"></span>
<span id="cb9-8"></span>
<span id="cb9-9">Uf1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, (x, y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb9-10">Uf2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, (x, y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb9-11">Uf3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, (x, y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb9-12">Uf4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">oracle</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, (x, y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">⊻</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-13"></span>
<span id="cb9-14"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@test</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(deutchJosza, [Uf1, Uf2, Uf3, Uf4]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> [<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">true</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span>]</span></code></pre></div></div>
<p>The line <code>reinterpret(Int, y.chunks)[1] != 0</code> converts the binary result to an integer and checks if it’s different from 0.</p>
<p>That’s the Deutch-Jozsa algorithm complete. Next one will be Grover’s algorithm, then Simon’s algorithm before we get into the QFT and finally Shor’s famous factoring algorithm.</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2019-06-17-building-a-qpu-simulator-in-julia-part-4/</guid>
  <pubDate>Mon, 17 Jun 2019 21:50:00 GMT</pubDate>
</item>
<item>
  <title>Illicit Negative Fallacy</title>
  <link>https://johnhearn.github.io/notes/2019-06-14-illicit-negative/</link>
  <description><![CDATA[ 




<p>A fallacy related to the <a href="https://www.logicallyfallacious.com/tools/lp/Bo/LogicalFallacies/94/False-Dilemma">False Dilemma</a> and the <a href="https://www.logicallyfallacious.com/tools/lp/Bo/LogicalFallacies/65/Cherry-Picking">Half Truth</a> but not really the same as either, the <a href="https://en.wikipedia.org/wiki/Affirmative_conclusion_from_a_negative_premise"><em>illicit negative</em></a> is everywhere.</p>
<p>Take for example the statement “<em>I believe God exists</em>”. We often implicitly or explicitly assume it has only one possible negative, “<em>I don’t believe God exists</em>”.</p>
<p>There are, in fact, three alternative statements: - I have no belief that God exists (weak atheism, or, sometimes, agnosticism, especially in the UK) - I believe that God does not exist (strong atheism) - I have no belief that God does not exist (An even weaker form of agnosticism form of agnosticism but as rational a statement as any other)</p>
<p>We can look at which of these statements are mutually exclusive (hint: the affirmative ones) but in any case, in many arguments about God (for example on Twitter or Quora) these distinctions will likely be forgotten. The reactions to David Mitchell’s <a href="https://youtu.be/xA85LVmqg0M">explanation of his agnosticism</a> are a good example of this happening.</p>
<p>These failures of logic, called logical fallacies, are everywhere from politics to family arguments. Take this <a href="https://youtu.be/NfFrW4yDubc?t=10">famous exchange</a> between Will Self and Mark Francois: &gt; Will: your problem, Mark, is not that you have be a racist or an anti-semite to vote for Brexit, it’s just that every racist and anti-semite in the country did.</p>
<blockquote class="blockquote">
<p>Mark: I think that’s a slur of 17.4 million people [who voted Brexit].</p>
</blockquote>
<p>We have a politician provoked into mixing up the negative forms of a statement to reach a non sequitur that makes the conversation valueless to the point of being funny.</p>
<p>Once you start thinking about it, <a href="https://rationalwiki.org/wiki/Confusion_of_the_inverse">confusion of the inverse</a> is everywhere.</p>



 ]]></description>
  <category>philosophy</category>
  <category>communication</category>
  <guid>https://johnhearn.github.io/notes/2019-06-14-illicit-negative/</guid>
  <pubDate>Fri, 14 Jun 2019 07:16:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 3</title>
  <link>https://johnhearn.github.io/notes/2019-05-24-building-a-qpu-simulator-in-julia-part-3/</link>
  <description><![CDATA[ 




<p>In the <a href="../2019-05-09-building-a-qpu-simulator-in-julia-part-2/">last post</a> we were able to build a functional quantum register and used it to create a simple circuit for a <a href="../2018-09-30-quantum-random-number-generator/">quantum random number generator</a>. It worked directly with registers up to about 11 bits when the amount of memory required to represent the operations on the register surpassed the available resources.</p>
<p>This short post is to see if we can improve the simple functions we wrote to increase the performance.</p>
<p>The first obvious thing to try is to use sparse matrices instead of dense ones. This is especially important when lifting the operators over large registers because the dense matrices are full of zeros. For example take a hadamard operator lift into the 2rd position of a 3-bit register. The lifted matrix becomes:</p>
<pre class="console"><code>julia&gt; lift(3, 2, H)
8×8 Array{Float32,2}:
 0.707107  0.0        0.707107   0.0       0.0       0.0        0.0        0.0     
 0.0       0.707107   0.0        0.707107  0.0       0.0        0.0        0.0     
 0.707107  0.0       -0.707107  -0.0       0.0       0.0       -0.0       -0.0     
 0.0       0.707107  -0.0       -0.707107  0.0       0.0       -0.0       -0.0     
 0.0       0.0        0.0        0.0       0.707107  0.0        0.707107   0.0     
 0.0       0.0        0.0        0.0       0.0       0.707107   0.0        0.707107
 0.0       0.0       -0.0       -0.0       0.707107  0.0       -0.707107  -0.0     
 0.0       0.0       -0.0       -0.0       0.0       0.707107  -0.0       -0.707107</code></pre>
<p>This matrix has a high density of zeros, something which indicates that a sparce representation may be better. Luckily <a href="https://julialang.org/">Julia</a> includes support for sparce matrices as part of the <code>SparseArrays</code> package and then we can simply start creating sparse matrices with a couple of simple changes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">SparseArrays</span></span>
<span id="cb2-2"></span>
<span id="cb2-3">ZERO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparsevec</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1f0</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-4">ONE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparsevec</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1f0</span>], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6">eye <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sparse</span>(I, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<pre class="console"><code>julia&gt; lift(3, 2, H)
8×8 SparseMatrixCSC{Float32,Int64} with 16 stored entries:
  [1, 1]  =  0.707107
  [3, 1]  =  0.707107
  [2, 2]  =  0.707107
  [4, 2]  =  0.707107
  [1, 3]  =  0.707107
  [3, 3]  =  -0.707107
  [2, 4]  =  0.707107
  [4, 4]  =  -0.707107
  [5, 5]  =  0.707107
  [7, 5]  =  0.707107
  [6, 6]  =  0.707107
  [8, 6]  =  0.707107
  [5, 7]  =  0.707107
  [7, 7]  =  -0.707107
  [6, 8]  =  0.707107
  [8, 8]  =  -0.707107</code></pre>
<p>We have reduced the memory footprint of the matrix by a factor of 4. In fact the saving is exponential: a <img src="https://latex.codecogs.com/png.latex?2%5En%20%5Ctimes%202%5En"> array becomes a sparse representation with <img src="https://latex.codecogs.com/png.latex?2%5E%7Bn+1%7D"> entries.</p>
<p>Trying the same benchmark again gives us much better results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span></span>
<span id="cb4-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@time</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measureAll</span>(n, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hadamard</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(n))[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb4-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<pre class="console"><code>0  0.000075 seconds (23 allocations: 1.047 KiB)
3  0.000034 seconds (41 allocations: 1.969 KiB)
0  0.000019 seconds (51 allocations: 3.016 KiB)
15  0.000029 seconds (73 allocations: 5.516 KiB)
12  0.000030 seconds (84 allocations: 11.422 KiB)
46  0.000038 seconds (102 allocations: 31.031 KiB)
62  0.000103 seconds (119 allocations: 102.750 KiB)
143  0.000190 seconds (134 allocations: 374.313 KiB)
301  0.000605 seconds (150 allocations: 1.396 MiB)
130  0.002381 seconds (159 allocations: 5.463 MiB)
666  0.016114 seconds (202 allocations: 21.679 MiB)</code></pre>
<p>Lets see how far we can go…</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span></span>
<span id="cb6-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@time</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measureAll</span>(n, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hadamard</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(n))[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb6-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<pre class="console"><code>1131  0.064576 seconds (199 allocations: 85.922 MiB)
430  0.296749 seconds (252 allocations: 342.593 MiB)
12847  1.547630 seconds (285 allocations: 1.336 GiB, 32.59% gc time)
24906  4.267627 seconds (317 allocations: 5.340 GiB, 4.51% gc time)
63277 39.087846 seconds (787 allocations: 21.372 GiB, 6.12% gc time)</code></pre>
<p>It turns out that we’ve improved the performance by a factor of about 32! Not a bad for a 3 line change :)</p>
<p>There are more things to do like in-place multiplications but I’d like to implement some of the standard algorithms first to compare performance in more varied circumstances. That’ll be for next time.</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2019-05-24-building-a-qpu-simulator-in-julia-part-3/</guid>
  <pubDate>Fri, 24 May 2019 06:22:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 2</title>
  <link>https://johnhearn.github.io/notes/2019-05-09-building-a-qpu-simulator-in-julia-part-2/</link>
  <description><![CDATA[ 




<p>In the <a href="../2019-05-07-building-a-qpu-simulator-in-julia-part-1/">last post</a> we saw how the Julia programming language has several advantages over Kotlin and Clojure for building a quantum computer simulator. This post covers how we might do that<sup>1</sup>. We’ll start as <a href="../2019-01-25-building-a-qpu-simulator-in-clojure-part-1/">before</a> with a simple case of an 8-sided <a href="../2018-09-30-quantum-random-number-generator/">quantum die</a>. This is the code written with the <a href="https://github.com/johnhearn/quko">Quko</a> library (from the <a href="https://github.com/johnhearn/quko">README</a>):</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Note: These notes go into some pretty heavy maths. For a less mathematical description of quantum computing concepts, take a look at <a href="../../posts/2018-08-16-quantum-computing-primer-part-1a/">this article</a>.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">qubits</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>hadamard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0..2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-2">print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>measureAll<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>toInt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span></code></pre></div></div>
<p>Written in Julia this becomes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1">qubits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hadamard</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubits</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measureAll</span>(qubits))</span></code></pre></div></div>
<p>Some things to notice. Firstly we’ve moved to a functional style. As typing is optional there is no need to define the variable. Also the range specified by <code>1:3</code> is using <a href="https://craftofcoding.wordpress.com/2017/03/12/why-1-based-indexing-is-ok/">1-based indexing</a>.</p>
<section id="measurement-of-single-qubit" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="measurement-of-single-qubit">Measurement of Single Qubit</h2>
<p>We’ll define a test to verify the probability that our qubit is measured <code>false</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1">repeatedly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, f) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n))</span>
<span id="cb3-2">sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, f) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">repeatedly</span>(n, f))</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@test</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubit</span>())) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div></div>
<p>As <a href="../2019-01-25-building-a-qpu-simulator-in-clojure-part-1/">before</a>, we can make that pass trivially, of course, defining a dummy qubit and just returning false from <code>measure</code> every time.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1">qubit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">nothing</span></span>
<span id="cb4-2">measure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (qubit) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">false</span></span></code></pre></div></div>
<p>Now, given Julia’s support for matrices, let’s jump straight to theory. A qubit can be represented by a <strong>pair</strong> of (complex) numbers, a <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BH%7D_2"> space. In the case of a qubit initialised to the <img src="https://latex.codecogs.com/png.latex?%5Cket%7B0%7D"> state that is just <code>[1 0]</code>. We saw in the <a href="../2019-05-07-building-a-qpu-simulator-in-julia-part-1/">last post</a> how easy that is in Julia<sup>2</sup>:</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;Julia uses 64-bit floats by default. We don’t need that level of precision and 32-bit floats save memory, a simple but important consideration for QPU simulators.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1">qubit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Float32</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span></code></pre></div></div>
<p>The measurement can be represented by a matrix operator using the quadratic form <img src="https://latex.codecogs.com/png.latex?p(%5Cket%7B0%7D)=%5Cbraket%7Bv%7D%7B0%7D%5Cbraket%7B0%7D%7Bv%7D=v%5E%7BT%7D%20M_0%20v"> where <img src="https://latex.codecogs.com/png.latex?p(%5Cket%7B0%7D)"> is the probability that a qubit is measured as <code>false</code> and</p>
<p><img src="https://latex.codecogs.com/png.latex?%20M_0%20=%20%5Cketbra%7B0%7D%7B0%7D%20=%20%5Cleft(%0A%20%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%201%20%5C%5C%0A%20%200%20%5C%5C%0A%20%20%5Cend%7Barray%7D%0A%20%20%5Cright)%0A%20%20%5Cleft(%0A%20%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%201%20&amp;%200%0A%20%20%5Cend%7Barray%7D%0A%20%20%5Cright)%0A%20%20=%0A%20%20%5Cleft(%0A%20%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%201%20&amp;%200%20%5C%5C%0A%20%200%20&amp;%200%20%5C%5C%0A%20%20%5Cend%7Barray%7D%0A%20%20%5Cright)%0A"></p>
<p>To make this calculation we can take advantage of Julia’s <a href="https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#Base.adjoint">adjoint</a> operator, <code>'</code>. Also this is where the random aspect of the qubit comes into play and the <code>rand()</code> function is used to determine if the measurement is indeed <code>false</code> based on the probability of it being so.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1">M₀<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' * [1 0]</span></span>
<span id="cb6-2">measure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (qubit) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> M₀ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q)</span></code></pre></div></div>
<pre class="console"><code>julia&gt; @test sample(1_000_000, () -&gt; measure(qubit())) == 0
Test Passed</code></pre>
</section>
<section id="the-hadamard-gate" class="level2">
<h2 class="anchored" data-anchor-id="the-hadamard-gate">The Hadamard Gate</h2>
<p>At the moment the qubit is always initialised to <img src="https://latex.codecogs.com/png.latex?%5Cket%7B0%7D"> so measurement will always return <code>false</code> with 100% probability. To make the measurement more meaningful we want to be able to move the state of the qubit into a 50/50 superposition (half way around the Bloch sphere) so that measurement will return <code>true</code> or <code>false</code> with equal probability, a fair coin toss. We can do that using the Hadamard operator. The Hadamard operator is defined as <img src="https://latex.codecogs.com/png.latex?H%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D%20%5Cleft(%0A%20%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%201%20&amp;%201%20%5C%5C%0A%20%201%20&amp;%20-1%20%5C%5C%0A%20%20%5Cend%7Barray%7D%0A%20%20%5Cright)"> and is similarly easy to implement with Julia’s matrix syntax:</p>
<pre class="console"><code>julia&gt; H = Float32(1/sqrt(2)) * [1 1; 1 -1]
2×2 Array{Float32,2}:
 0.707107   0.707107
 0.707107  -0.707107

julia&gt; hadamard = (qubit) -&gt; H * qubit
julia&gt; hadamard(qubit())
2-element Array{Float32,1}:
 0.70710677
 0.70710677</code></pre>
<p>Or even better using the piping operator:</p>
<pre class="console"><code>julia&gt; qubit() |&gt; hadamard
2-element Array{Float32,1}:
 0.70710677
 0.70710677</code></pre>
<p>Then we can measure our qubit and it gives us one random bit each time, as can be seen by measuring multiple qubits prepared in the same way.</p>
<pre class="console"><code>julia&gt; repeatedly(3, () -&gt; qubit() |&gt; hadamard |&gt; measure)
10-element Array{Bool,1}:
 false
  true
 false</code></pre>
<p>We could build our random number generator already, taking 3 independent measurements and interpreting it as binary. For example,</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb11-1">coinToss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubit</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> hadamard <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> measure</span>
<span id="cb11-2">toInt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (b) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>((acc, v) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v ? <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, b)</span></code></pre></div></div>
<pre class="console"><code>julia&gt; toInt(repeatedly(3, coinToss))
6
julia&gt; toInt(repeatedly(3, coinToss))
3</code></pre>
<p>This, however, is cheating because we can only deal with one qubit at a time and it doesn’t implement our original feature test. We’re back to the same point as we were with the Clojure implementation but in a better position because now we can take advantage of more of Julia’s built-in linear algebra support.</p>
</section>
<section id="quantum-registers" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="quantum-registers">Quantum Registers</h2>
<p>So let’s make things even harder and represent qubit <em>registers</em>. Registers are <em>systems</em> of qubits in a combined state. We calculate this combined state with the <a href="https://en.wikipedia.org/wiki/Kronecker_product">Krondecker</a> product, also implemented natively in Julia by the <a href="https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#Base.kron"><code>kron</code></a> function. Let’s try a 2 qubit register:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb13-1">qubits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kron</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubit</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubit</span>())</span></code></pre></div></div>
<p>Testing in the console, it just works…</p>
<pre class="console"><code>julia&gt; qubits()
4-element Array{Float32,1}:
 1.0
 0.0
 0.0
 0.0</code></pre>
<p>We can generate any size register we please, with the only constraint being available memory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1">register <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qubit</span>(), n))</span></code></pre></div></div>
<p>This function uses the standard <code>fold</code> function to perform the <code>kron</code> operation, from left to right, over an array of <code>n</code> qubits.</p>
<pre class="console"><code>julia&gt; register(3)
8-element Array{Float32,1}:
 1.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0</code></pre>
<p>It works as expected. A 2 bit register needs an array of 4 complex numbers, a 3 bit one needs an 8 bit array, 4-bits require 16 and so on. The size of the array gets very big.</p>
<p>Now we can turn our attention to the quantum gates and combine them into a multiple qubit gate. Once again the Krondecker product is used and works in exactly the same way.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb17-1">hadamard <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(H, n))</span></code></pre></div></div>
<pre class="console"><code>julia&gt; hadamard(3)
8×8 Array{Float32,2}:
 0.353553   0.353553   0.353553   0.353553   0.353553   0.353553   0.353553   0.353553
 0.353553  -0.353553   0.353553  -0.353553   0.353553  -0.353553   0.353553  -0.353553
 0.353553   0.353553  -0.353553  -0.353553   0.353553   0.353553  -0.353553  -0.353553
 0.353553  -0.353553  -0.353553   0.353553   0.353553  -0.353553  -0.353553   0.353553
 0.353553   0.353553   0.353553   0.353553  -0.353553  -0.353553  -0.353553  -0.353553
 0.353553  -0.353553   0.353553  -0.353553  -0.353553   0.353553  -0.353553   0.353553
 0.353553   0.353553  -0.353553  -0.353553  -0.353553  -0.353553   0.353553   0.353553
 0.353553  -0.353553  -0.353553   0.353553  -0.353553   0.353553   0.353553  -0.353553</code></pre>
<p>It’s clear how the size of these gates gets out of hand very quickly having a size of <img src="https://latex.codecogs.com/png.latex?2%5En%20%5Ctimes%202%5En">.</p>
<p>Using this gate we can apply the Hadamard operator to all the qubits at once.</p>
<pre class="console"><code>julia&gt; hadamard(3) * register(3)
8-element Array{Float32,1}:
 0.35355335
 0.35355335
 0.35355335
 0.35355335
 0.35355335
 0.35355335
 0.35355335
 0.35355335</code></pre>
<p>This is the combined state of a 3 bit quantum register with each bit in a 50/50 superposition. Not to be confused with entanglement because the bits are still independent… so far.</p>
<p>This is where things get a little more tricky. To measure a single qubit we need to place the measurement operator over the qubit in the register, applying the identity operator for all other bits. This is called lifting<sup>3</sup>.</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;At least it’s called lifting in <a href="https://arxiv.org/abs/1608.03355">this paper</a> which I found helpful to understand this process.</p></div></div><p>We’ll need the identity operator for a single qubit <img src="https://latex.codecogs.com/png.latex?I%20=%20%5Cleft(%0A%20%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%201%20&amp;%200%20%5C%5C%0A%20%200%20&amp;%201%20%5C%5C%0A%20%20%5Cend%7Barray%7D%0A%20%20%5Cright)">:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb20-1">eye <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Matrix</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{Float32}</span>(I, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<p>Now we must <em>lift</em> the <img src="https://latex.codecogs.com/png.latex?M_0"> operator as <img src="https://latex.codecogs.com/png.latex?I%5E%7Bk-1%7D%20%5Ctimes%20M_0%20%5Ctimes%20I%5E%7Bn-k-1%7D">. In Julia this can be written like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb21-1">lift <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, k, op) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> (it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> k) ? op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eye, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n)))</span></code></pre></div></div>
<p>This function will combine a series of <img src="https://latex.codecogs.com/png.latex?I"> operators, the <code>op</code> operator over the <code>k</code>th bit and then more <img src="https://latex.codecogs.com/png.latex?I"> operators to the end of the register. The result is<sup>4</sup>:</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;It can be seen here that the lifting matrix is mostly full of zeros. Hopefully we’ll be able to take advantage of Julia’s specialised matrix representations to optimise this.</p></div></div><pre class="console"><code>julia&gt; lift(3, 2, M₀)
8×8 Array{Float32,2}:
 1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0</code></pre>
<p>Measurement of the kth bit is then:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb23-1">measure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, k, qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lift</span>(n, k, M₀) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qubits</span></code></pre></div></div>
<p>So we can measure the 2nd bit like this:</p>
<pre class="console"><code>julia&gt; qubits = hadamard(3) * register(3)
...
julia&gt; measure(3, 2, qubits)
true</code></pre>
<p>One more important point. The measurement of each qubit is an operation which <em>collapses</em> the state of the qubit, i.e.&nbsp;changes its value. The measurement function must <em>lift</em> the <img src="https://latex.codecogs.com/png.latex?M_0"> or <img src="https://latex.codecogs.com/png.latex?M_1"> operation (depending on the result) over the measured qubit, apply it to the register and then renormalise it. More information about this can be found in <a href="https://arxiv.org/abs/1608.03355">this paper</a>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb25-1">M₁ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' * [0 1]</span></span>
<span id="cb25-2">measure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, k, qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">begin</span></span>
<span id="cb25-3">       result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lift</span>(n, k, M₀) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qubits</span>
<span id="cb25-4">       (result, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lift</span>(n, k, (result ? M₁ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> M₀)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qubits))</span>
<span id="cb25-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>To measure all the bits at once we can do something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb26-1">measureAll <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">begin</span></span>
<span id="cb26-2">         results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []; </span>
<span id="cb26-3">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb26-4">           result, qubits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(n, k, qubits)</span>
<span id="cb26-5">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push!</span>(results, result)</span>
<span id="cb26-6">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb26-7">         results, qubits</span>
<span id="cb26-8">       <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<p>It’s a bit nasty but, save for a few indices, at last we can say we have completed the first feature test. So our final solution, after extracting some constants, is:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb27-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">using</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">LinearAlgebra</span></span>
<span id="cb27-2"></span>
<span id="cb27-3">ZERO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Float32</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb27-4">ONE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Float32</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb27-5"></span>
<span id="cb27-6">eye <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Matrix</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{Float32}</span>(I, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb27-7">H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Float32</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>; <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb27-8">M₀ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ZERO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ZERO<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb27-9">M₁ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ONE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ONE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb27-10"></span>
<span id="cb27-11">register <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(ZERO, n))</span>
<span id="cb27-12">hadamard <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(H, n))</span>
<span id="cb27-13"></span>
<span id="cb27-14">lift <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, k, op) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foldl</span>(kron, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> (it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> k) ? op <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> eye, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n)))</span>
<span id="cb27-15"></span>
<span id="cb27-16">measure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, k, qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">begin</span></span>
<span id="cb27-17">         result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rand</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lift</span>(n, k, M₀) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qubits</span>
<span id="cb27-18">         (result, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalize</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lift</span>(n, k, (result ? M₁ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> M₀)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qubits))</span>
<span id="cb27-19">       <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb27-20"></span>
<span id="cb27-21">measureAll <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, qubits) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">begin</span></span>
<span id="cb27-22">         results <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [];</span>
<span id="cb27-23">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n</span>
<span id="cb27-24">           result, qubits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measure</span>(n, k, qubits)</span>
<span id="cb27-25">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">push!</span>(results, result)</span>
<span id="cb27-26">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb27-27">         results, qubits</span>
<span id="cb27-28">       <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb27-29"></span>
<span id="cb27-30">toInt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (b) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reduce</span>((acc, v) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (v ? <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), b, init<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb27-31"></span>
<span id="cb27-32">qubits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hadamard</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb27-33"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measureAll</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, qubits)[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div></div>
<p>Running it to give us a random number from 0 to 7 on the console.</p>
<p>Before we leave it, what’s the performance of this thing in its current form? Let’s see:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb28-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span></span>
<span id="cb28-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">@time</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toInt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">measureAll</span>(n, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hadamard</span>(n) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">register</span>(n))[<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb28-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span></code></pre></div></div>
<pre class="console"><code>1  0.834134 seconds (1.63 M allocations: 90.474 MiB, 2.38% gc time)
0  0.063235 seconds (15.49 k allocations: 813.162 KiB)
7  0.004201 seconds (9.11 k allocations: 281.986 KiB)
11  0.002534 seconds (27.13 k allocations: 623.813 KiB)
30  0.011686 seconds (122.15 k allocations: 2.509 MiB)
7  0.023647 seconds (634.68 k allocations: 12.326 MiB)
1  0.086709 seconds (3.05 M allocations: 58.193 MiB, 5.11% gc time)
185  0.362644 seconds (14.04 M allocations: 265.855 MiB, 1.72% gc time)
237  1.746489 seconds (63.02 M allocations: 1.163 GiB, 6.29% gc time)
504  7.655970 seconds (279.03 M allocations: 5.145 GiB, 11.96% gc time)
490 31.689085 seconds (1.29 G allocations: 23.612 GiB, 4.77% gc time)</code></pre>
<p>At just 11 bits it’s taking up huge parts of my machine and far exceeding available main memory. Now we have the machinery written in Julia we can try a number of things to improve performance:</p>
<ul>
<li>We’re using dense matrices everywhere. We can improve that by using sparse matrices where appropriate. The lifting matrix., especially, is open for conversion to a sparse matrix representation.</li>
<li>Since the vectors can become quite large it makes sense to do the multiplications in-place, breaking the immutability of the functional paradigm but improving the performance and memory usage which will be very important for larger registers. May be necessary to write our own algorithm to do that using sparse matrices and dense arrays.</li>
</ul>
<p>Also there are a number of other improvements to consider: - We pass around the number of bits when it could be determined implicitly from the size of the arrays. - There is no entanglement yet in our system, for that we need to define a Conditional NOT operator and for that a more sophisticated <code>lift</code> function.</p>
<p>We’ll cover all that in another post.</p>


</section>


 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>software-testing</category>
  <guid>https://johnhearn.github.io/notes/2019-05-09-building-a-qpu-simulator-in-julia-part-2/</guid>
  <pubDate>Tue, 07 May 2019 18:42:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Julia - Part 1</title>
  <link>https://johnhearn.github.io/notes/2019-05-07-building-a-qpu-simulator-in-julia-part-1/</link>
  <description><![CDATA[ 




<p>Just for a kicks, I started rewriting <a href="https://github.com/johnhearn/quko">Quko</a>, my simple quantum computing simulator, <a href="../2019-01-25-building-a-qpu-simulator-in-clojure-part-1/">in clojure</a>. I thought that having powerful linear algebra libraries like <a href="https://neanderthal.uncomplicate.org/">Neanderthal</a> would help simplify the task but it didn’t feel right. I decided to put it on hold until I got some inspiration.</p>

<div class="no-row-height column-margin column-container page-columns page-full"><div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2019-05-07-building-a-qpu-simulator-in-julia-part-1/pointcare-snns.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">The talk included Julia code for generating this Pointcaré section of the chaotic interaction between 3 spiking neural networks (SNNs) which I, ironically, converted to Kotlin.</figcaption>
</figure>
</div></div><p>So, I started looking again at <a href="../../posts/2019-04-20-encounter-with-complexity/"><em>complexity</em> theory</a>. I was lead via <a href="https://en.wikipedia.org/wiki/Logistic_map">logistic maps</a>, and <a href="https://en.wikipedia.org/wiki/Lotka%E2%80%93Volterra_equations">other</a> <a href="https://en.wikipedia.org/wiki/Rayleigh%E2%80%93B%C3%A9nard_convection">dynamic</a> <a href="https://en.wikipedia.org/wiki/Galaxy_filament">systems</a> to a library called <a href="https://juliadynamics.github.io/JuliaDynamics/">DynamicSystems.jl</a> written in a language called <a href="https://julialang.org/">Julia</a>. I remembered having seen Julia two years ago from another <a href="https://www.youtube.com/watch?v=cLLQcshWEbE">talk</a> on simulating spiking neural networks using Julia.</p>
<p>The things that were bothering me in Kotlin and Clojure when building the QPU simulator were complex numbers and linear algebra, specifically the Krondecker product, and it turns out that Julia supports both natively! So it was worth giving it a try<sup>1</sup>.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Edit: For a nice guide through <a href="https://julialang.org/">Julia</a> syntax take a look at the <a href="https://benlauwens.github.io/ThinkJulia.jl/latest/book.html">ThinkJulia online book</a>. It pretty much covers all the basic and is very easy to understand.</p></div><div id="fn2"><p><sup>2</sup>&nbsp;You can use it from <a href="https://jupyter.org/">Jupyter</a>, if you prefer. I wrote down how to do that in a <a href="../2019-05-05-using-julia-in-jupyter/">separate note</a>. The community overlap with Python is understandable.</p></div></div><p>Installing Julia on a Mac is as simple as you might expect<sup>2</sup></p>
<pre class="console"><code>&gt; brew install julia
...</code></pre>
<p>Like Clojure, Julia has a nice REPL for developing and testing ideas quickly. Built-in support for vectors means that defining a qubit is easy<sup>3</sup>:</p>
<div class="no-row-height column-margin column-container"><div id="fn3"><p><sup>3</sup>&nbsp;Julia uses 64-bit floats by default. We don’t need that level of precision and 32-bit floats save memory, a simple but important consideration for QPU simulators.</p></div></div><pre class="console"><code>julia&gt; qubit = Float32(1) * [1, 0]
2-element Array{Float32,1}:
 1.0
 0.0</code></pre>
<p>Julia has dynamic typing so the variable type doesn’t need to be set explicitly. The Hadamard operator[^hadamard] is similarly easy to define with Julia’s matrix syntax:</p>
<pre class="console"><code>julia&gt; H = Float32(1/sqrt(2)) * [1 1; 1 -1]
2×2 Array{Float32,2}:
 0.707107   0.707107
 0.707107  -0.707107

julia&gt; H * qubit
2-element Array{Float32,1}:
 0.70710677
 0.70710677</code></pre>
<p>Taking it a step further we can use complex numbers natively. For example, we are able to define the <a href="https://en.wikipedia.org/wiki/Quantum_logic_gate#Square_root_of_NOT_gate_(%E2%88%9ANOT)"><img src="https://latex.codecogs.com/png.latex?%5Csqrt%7BNOT%7D"> gate</a>[^sqrtnot] almost as easily:</p>
<pre class="console"><code>julia&gt; halfX = Float32(0.5) * [1+im 1-im; 1-im 1+im]
2×2 Array{Complex{Float32},2}:
 0.5+0.5im  0.5-0.5im
 0.5-0.5im  0.5+0.5im

julia&gt; halfX * qubit
2-element Array{Complex{Float32},1}:
 0.5f0 + 0.5f0im
 0.5f0 - 0.5f0im</code></pre>
<p>We can test that it’s unitary using the conjugation operator, <code>'</code>, and the <code>@test</code> macro:</p>
<pre class="console"><code>julia&gt; using Test
julia&gt; @test halfX'*halfX ≈ [1 0; 0 1]
Test Passed</code></pre>
<p>It’s normal in Julia to use Unicode characters directly. This is strange at first but makes formulas very succinct. In the above test we used the <code>≈</code> character<sup>4</sup> to perform a machine-precision equality test.</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;In the REPL, type <code>\approx</code> and then press <code>Tab</code>.</p></div></div><p>Being a functional language, it’s straightforward to turn our operators into functions. X is the quantum NOT operator:</p>
<pre class="console"><code>julia&gt; X = (qubit) -&gt; Float32(1) * [0 1; 1 0] * qubit
#3 (generic function with 1 method)

julia&gt; @test X(X(qubit)) ≈ qubit
Test Passed</code></pre>
<p>This language really is a close to perfect for my project! <a href="https://cognitive-edge.com/blog/serendipity/">Serendipity</a>[^serendipity] is a wonderful thing.</p>




 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>software-testing</category>
  <guid>https://johnhearn.github.io/notes/2019-05-07-building-a-qpu-simulator-in-julia-part-1/</guid>
  <pubDate>Tue, 07 May 2019 18:42:00 GMT</pubDate>
</item>
<item>
  <title>Using Julia From Jupyter</title>
  <link>https://johnhearn.github.io/notes/2019-05-05-using-julia-in-jupyter/</link>
  <description><![CDATA[ 




<p>For Jupyter you want Python 3.7. Mac comes with 2.7 by default. To switch environments install Anaconda and then Jupyter itself:</p>
<pre class="console"><code>&gt; brew cask install anaconda
...
&gt; brew postinstall python3
...
&gt; brew install jupyter
...</code></pre>
<p>To use Julia inside Jupyter you need to have Julia itself installed locally, if you don’t then install it with Homebrew:</p>
<pre class="console"><code>&gt; brew cask install julia
...</code></pre>
<p>To link it up then go into the Julia REPL <code>julia</code> and then import the <code>IJulia</code> package:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1">julia<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Pkg;</span> Pkg.add("IJulia")</span></code></pre></div></div>
<p>Then should then be able to run Jupyter <code>jupyter notebook</code> and Julia will be amongst the options when creating a new notebook.</p>



 ]]></description>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2019-05-05-using-julia-in-jupyter/</guid>
  <pubDate>Sun, 05 May 2019 17:30:00 GMT</pubDate>
</item>
<item>
  <title>COBOL for Fun and Profit</title>
  <link>https://johnhearn.github.io/notes/2019-05-03-cobol-for-fun-and-profit/</link>
  <description><![CDATA[ 




<p>I’m lucky enough to have worked in many different areas of the IT industry and several times I’ve crossed paths with COBOL both in the public and the financial sectors. There is no doubt that, in spite of the total absence of COBOL from Stack Overflow’s annual <a href="https://insights.stackoverflow.com/survey/2019#technology-_-programming-scripting-and-markup-languages">Developer Survey</a>, it’s still very much alive, and it’s behind a <a href="http://fingfx.thomsonreuters.com/gfx/rngs/USA-BANKS-COBOL/010040KH18J/index.html">staggering amount</a> of the systems that our society relies on - banking, insurance and central government, to name a few.</p>
<p>After seeing <a href="https://increment.com/programming-languages/cobol-all-the-way-down/">an article</a> come up again on the subject it got me thinking about its title caption: <em>how long can it be maintained?</em> Or, more concretely, <em>can I compile and run a COBOL program on my laptop?</em> I’d only ever see it running on Mainframes behind green-screen terminals and choose-your-own-adventure-style <a href="https://www.ibm.com/support/knowledgecenter/zosbasics/com.ibm.zos.zconcepts/zconc_whatistso.htm">TSO</a> screens. Well, it turns out the answer is YES, and in fact it’s very simple. This is how I did it.</p>
<section id="gnucobol" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="gnucobol">GnuCOBOL</h2>
<p><a href="https://sourceforge.net/projects/open-cobol/">GnuCOBOL</a> compiles COBOL to executable binaries. It actually does this by transpiling to C but that doesn’t really concern me.</p>
<p>First we need to install the compiler. You can just do:</p>
<pre class="console"><code>&gt; brew install gnu-cobol</code></pre>
<p>Seriously, it’s that easy.</p>
<p>Now, to edit our COBOL programs do we have to resort to some archaic editor? Well, not unless you think VS Code is archaic. It has a <a href="https://marketplace.visualstudio.com/items?itemName=bitlang.cobol">COBOL Plugin</a> with syntax highlighting and autocomplete.</p>
<div class="quarto-figure quarto-figure-center page-columns page-full">
<figure class="figure page-columns page-full">
<p><img src="https://johnhearn.github.io/notes/2019-05-03-cobol-for-fun-and-profit/vscode-cobol.png" class="img-fluid figure-img"></p>
<figcaption class="margin-caption">COBOL in VS Code</figcaption>
</figure>
</div>
<p>This is a basic “Hello World!” program for COBOL taken from the article:</p>
<pre style="font-family:IBM3720;">000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLO-WORLD.
000300 PROCEDURE DIVISION.
000400      DISPLAY 'Hello, world!'.
000500      STOP RUN.

</pre>
<p>We can now compile it to a executable binary and run it directly:</p>
<pre class="console"><code>&gt; cobc -x hello.cob
&gt; ./hello
Hello, world!</code></pre>
<p>Mission completed. Really much simpler that I had expected.</p>
<p>So, if we can take COBOL and compile it to native binaries then couldn’t we take, for example, existing CICS transactions written for standard mainframes and deploy them as services, for example, in a <a href="../../posts/2018-12-29-serverless-is-the-new-mainframe/">serverless</a> environment?</p>
<p>So in answer to the question: <em>how long can it be maintained?</em>, based on what I’ve seen the answer is probably, for better or worse, <em>indefinitely</em>.</p>


</section>

 ]]></description>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2019-05-03-cobol-for-fun-and-profit/</guid>
  <pubDate>Fri, 03 May 2019 22:30:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Clojure - Part 2</title>
  <link>https://johnhearn.github.io/notes/2019-05-01-building-a-qpu-simulator-in-clojure-part-2/</link>
  <description><![CDATA[ 




<p>In the <a href="../2019-01-25-building-a-qpu-simulator-in-clojure-part-1/">last post</a> we simulated a single qubit and a couple of simple gates. Using those gates we could implement a trivial coin toss function and an 8-sided die. Now we’ll go on to incorporate the Neanderthal library before implementing more gates with complex matrices.</p>
<p>First let’s make the leap to Neanderthal so that it can do our matrix manipulations for us. It turns out to be quite straight forward. The first step is to add the dependency:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb1-1">(defproject qucl <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.1.0-SNAPSHOT"</span></span>
<span id="cb1-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:dependencies</span> [[org.clojure/clojure <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.10.0"</span>]</span>
<span id="cb1-3">                 [uncomplicate/neanderthal <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.23.1"</span>]]</span>
<span id="cb1-4"></span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:exclusions</span> [[org.jcuda/jcuda-natives <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:classifier</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"apple-x86_64"</span>]</span>
<span id="cb1-6">               [org.jcuda/jcublas-natives <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:classifier</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"apple-x86_64"</span>]])</span></code></pre></div></div>
<p>A couple of the libraries required by Neandertal don’t have native builds for Mac in the standard repositories which gives us dependency errors. Since their not needed right now, we can safely exclude them.</p>
<p>To work with Neanderthal the Qubit will be a vector of floating point numbers <code>fv</code>, rather than a map. The choice of floats rather than doubles is deliberate, floats are faster but, more importantly, take up less memory than doubles and the exponential nature of quantum vectors means that quantum simulators tend to be memory bound.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb2-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> Qubit </span>[]</span>
<span id="cb2-2">  (fv <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>))</span></code></pre></div></div>
<p>For the measurement we’ll just use the first entry of the vector:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb3-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> prob-zero </span>[qubit]</span>
<span id="cb3-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (entry qubit <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) (entry qubit <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)))</span></code></pre></div></div>
<p>And for the gates we’ll use Neanderthal’s built-in matrix multiplication functions, in this case multiplying a dense matrix (created with the <code>fge</code> function) with the qubit vector using the <code>mv</code> function. For example or X gate becomes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb4-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>[qubit]</span>
<span id="cb4-2">  (mv (fge <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) qubit))</span></code></pre></div></div>
<p>And the Hadamard gate is similar but scaling with the <em>in-place</em> <code>scal!</code> function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb5-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> H </span>[qubit]</span>
<span id="cb5-2">  (mv (scal! oosr2 (fge <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])) qubit))</span></code></pre></div></div>
<p>With these changes the tests continue to function without change and we’ve been able to remove own own linear algebra implementation and replace it with Neanderthal instead. Happy with that.</p>
<p>The next step is to build some more gates but for that we’ll need to use complex numbers. Essentially a quantum computing simulation is linear algebra using complex variables. Neanderthal is a fast and efficient linear algebra library but it uses doubles and floats as approximations of reals so we need a way to manipulate complex vectors and matrices. How can we use Neanderthal to do that? Well first note that a complex vector can be separated into real and imaginary parts. We can take a pair of matrices or vectors and store them in a simple map of real and imaginary values.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb6-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> complexify</span></span>
<span id="cb6-2">  ([real]</span>
<span id="cb6-3">   {<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> real <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> (zero real)})</span>
<span id="cb6-4">  ([real imag]</span>
<span id="cb6-5">   {real real <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> imag}))</span></code></pre></div></div>
<p>If we receive only one initial value then assume it’s the real part and set the imaginary part to zero. Once we have the separate real and imaginary values we can multiply, for example <code>A</code> and <code>x</code> like this:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA%20x%20=%20(A_r%20+%20A_i%20i)(x_r%20+%20x_i%20i)%0A=%20(A_r%20x_r%20-%20A_i%20x_i)%20+%20(A_r%20x_i%20+%20A_i%20x_r)%20i%0A"></p>
<p>In computational terms, that’s four floating point multiplications and three floating point additions. We can take advantage of Neanderthal’s compound add and multiply function <code>axpv</code> as well. Let’s see what that looks like in code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb7-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> complex-mv </span>[A x]</span>
<span id="cb7-2">  (complexify (axpy -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> (mv (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> x)) (mv (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> x)))</span>
<span id="cb7-3">              (axpy (mv (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> x)) (mv (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> x)))))</span></code></pre></div></div>
<p>A bit complicated but there is a lot of symmetry there. We use the new <code>complexify</code> function to convert the Qubit along with the H and X operators into complex valued functions:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb8-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> Qubit </span>[]</span>
<span id="cb8-2">  (complexify (fv <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)))</span>
<span id="cb8-3"></span>
<span id="cb8-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>[qubit]</span>
<span id="cb8-5">  (complex-mv (complexify (fge <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])) qubit))</span>
<span id="cb8-6"></span>
<span id="cb8-7">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> H </span>[qubit]</span>
<span id="cb8-8">  (complex-mv (complexify (scal! oosr2 (fge <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))) qubit))</span></code></pre></div></div>
<p>Also the probability calculation must be adapted to calculate the absolute value of a complex number:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb9-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> sqr </span>[x] (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> x x))</span>
<span id="cb9-2">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> prob-zero </span>[qubit]</span>
<span id="cb9-3">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (sqr (entry (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:real</span> qubit) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)) (sqr (entry (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:imag</span> qubit) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))))</span></code></pre></div></div>
<p>Our tests are passing again using the new complex implementation. So far so good. We’re now in a position to implement some other gates. But we have a problem. Neanderthal doesn’t have a mechanism for performing the Krondecker product and we’ll need that for creating the composite operators. Not sure what to do about that so I’m going to leave it there for now until I come up with a better plan ;)</p>



 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>mathematics</category>
  <guid>https://johnhearn.github.io/notes/2019-05-01-building-a-qpu-simulator-in-clojure-part-2/</guid>
  <pubDate>Wed, 01 May 2019 09:52:00 GMT</pubDate>
</item>
<item>
  <title>Building a QPU simulator in Clojure - Part 1</title>
  <link>https://johnhearn.github.io/notes/2019-01-25-building-a-qpu-simulator-in-clojure-part-1/</link>
  <description><![CDATA[ 




<p><a href="../../posts/2018-08-16-quantum-computing-primer-part-1a/">Last year</a> I started looking into quantum computing<sup>1</sup> and the result was <a href="https://github.com/johnhearn/quko">Quko</a>, a naïve quantum computer simulator written in Kotlin. If there’s one thing I learned from that project it’s that there is no better way to learn how something works than by writing a simulator for it: it turned out that things that I thought I understood in fact I didn’t until getting in to the nitty-gritty.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;If you happen to be a physics graduate and a programmer (there are more of us than you might think) then I guess it’s natural that you’ll eventually look into quantum computing at some point.</p></div><div id="fn2"><p><sup>2</sup>&nbsp;From <a href="https://www.youtube.com/watch?v=um2uq5oURT8">this</a> talk where they’re using Neanderthal for processing large neural networks.</p></div><div id="fn3"><p><sup>3</sup>&nbsp;although not so much with its naming conventions :/</p></div></div><p>Quko worked out pretty well and I was able to repeat some of the standard results with a small number of qubits. A naïve implementation will never be particularly efficient but I didn’t really care as that wasn’t the goal. Not long ago, however, I stumbled upon a Clojure library called <a href="https://neanderthal.uncomplicate.org/">Neanderthal</a><sup>2</sup> for doing high performance mathsy stuff and after playing with it for a while I was impressed by how easy it was to get started<sup>3</sup>. Since I’m also learning Clojure I thought I’d rewrite Quko in Clojure to practice the language and get a more powerful simulator to boot. This blog series will be my way of remembering what I did, the mistakes and the successes along the way.</p>
<p>We’ll start with a simple case of an 8-sided <a href="../2018-09-30-quantum-random-number-generator/">quantum die</a>. This is the code written with the Quko library (from the <a href="https://github.com/johnhearn/quko">README</a>):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">qubits</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>hadamard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0..2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-2">print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>qubits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>measureAll<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>toInt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span></code></pre></div></div>
<p>Written in Clojure this becomes<sup>4</sup>:</p>
<div class="no-row-height column-margin column-container"><div id="fn4"><p><sup>4</sup>&nbsp;Note I’ve changed the name of the Hadamard operator to H which is acceptable in Clojure and more consistent with the QC literature.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb2-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">def</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> qubits </span>(H (Qubits <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb2-2">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">print</span> (to-int (measure-all qubits)))</span></code></pre></div></div>
<p>Obviously this doesn’t compile because we’ve not written any code yet (see appendix) so lets start building out the implementation with unit tests. In fact we will start with a very simple test for a single qubit which ensures that its default value is 100% <code>true</code>. Remember qubits are probabilistic animals but we don’t want to expose the underlying implementation to the outside world. For that reason in the test we’ll take a count of multiple samples and compare the result with what’s expected rather than interrogating the qubit’s internals directly.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb3-1">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ns</span> qucl.qubits_test</span>
<span id="cb3-2">  (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:require</span> [clojure.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">test</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:refer</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:all</span>]</span>
<span id="cb3-3">            [qucl.qubits <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:refer</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:all</span>]))</span>
<span id="cb3-4"></span>
<span id="cb3-5">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> sample </span>[source-function num-samples]</span>
<span id="cb3-6">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">count</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">filter</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true?</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">repeatedly</span> num-samples source-function))))</span>
<span id="cb3-7"></span>
<span id="cb3-8">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">deftest</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> measure-should</span></span>
<span id="cb3-9">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (sample-qubit #(measure (Qubit)) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)))</span></code></pre></div></div>
<p>We can make that pass trivially, of course, just returning <code>0</code> from <code>measure</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb4-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> Qubit </span>[])</span>
<span id="cb4-2"></span>
<span id="cb4-3">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> measure </span>[qubit] <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span>)</span></code></pre></div></div>
<p>Next we’ll triangulate to get some more behaviour. The simplest way to do that is to apply a <code>not</code> or <code>X</code> operation<sup>5</sup> and expect the opposite result.</p>
<div class="no-row-height column-margin column-container"><div id="fn5"><p><sup>5</sup>&nbsp;The so called <a href="https://en.wikipedia.org/wiki/Quantum_logic_gate#Pauli-X_gate">Pauli-X gate</a>.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb5-1">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> (sample-qubit #(measure (X (Qubit))) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>))))</span></code></pre></div></div>
<p>The easiest way to make this pass is to make <code>Qubit</code> have a binary value and negate it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb6-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> Qubit </span>[] <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span>)</span>
<span id="cb6-2"></span>
<span id="cb6-3">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>[qubit]</span>
<span id="cb6-4">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> qubit)</span>
<span id="cb6-5"></span>
<span id="cb6-6">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> measure </span>[qubit]</span>
<span id="cb6-7">  qubit)</span></code></pre></div></div>
<p>The tests pass. One nice thing about Clojure is that we can also take advantage of the REPL to try our code. In this case we get the answers we expect.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb7-1">(measure (Qubit))</span>
<span id="cb7-2">=&gt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb7-3">(measure (X (Qubit)))</span>
<span id="cb7-4">=&gt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb7-5">(measure (X (X (Qubit))))</span>
<span id="cb7-6">=&gt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div></div>
<p>Now we have our tests passing we can introduce some theory. The qubit is a structure with two (possibly complex) variables<sup>6</sup>, the absolute values of which are the probabilities of measuring <code>0</code> or <code>1</code> respectively. For the moment the variable will only need real values and is represented by two real numbers <code>:0</code> and <code>:1</code>. This could be considered a vector with named indices. The <code>X</code> operation swaps the variables so the probability of measuring <code>0</code> becomes the probability of measuring <code>1</code> and vice-versa.</p>
<div class="no-row-height column-margin column-container"><div id="fn6"><p><sup>6</sup>&nbsp;A so called Hilbert space, <img src="https://latex.codecogs.com/png.latex?%20%5Cmathcal%7BH%7D_2%20">.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb8-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> Qubit </span>[]</span>
<span id="cb8-2">  {<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>})</span>
<span id="cb8-3"></span>
<span id="cb8-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>[qubit]</span>
<span id="cb8-5">  {<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> qubit) <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> qubit)})</span></code></pre></div></div>
<p>The measurement will now be a random process which picks <code>0</code> or <code>1</code> with the appropriate frequency. The probability of measuring a <code>0</code> is <code>|:0|</code><img src="https://latex.codecogs.com/png.latex?%20%5E2%20">.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb9-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> prob-zero </span>[qubit]</span>
<span id="cb9-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> qubit) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> qubit)))</span>
<span id="cb9-3"></span>
<span id="cb9-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> measure </span>[qubit]</span>
<span id="cb9-5">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&lt;=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">rand</span>) (prob-zero qubit)) <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span>))</span></code></pre></div></div>
<p>The implementation of <code>X</code> is a degenerate case of a more general fact. One of the central tenets of quantum operators or <em>gates</em> is that they can be represented by matrix multiplication<sup>7</sup>. Let’s do it that way so that we can slot in other gates much more easily. We define a matrix which inverts the two numbers:</p>
<div class="no-row-height column-margin column-container"><div id="fn7"><p><sup>7</sup>&nbsp;As can any operator according to <a href="https://en.wikipedia.org/wiki/Representation_theory">representation theory</a>.</p></div></div><p><img src="https://latex.codecogs.com/png.latex?%0AX%20=%20%5Cleft(%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%20%20%20%20%200%20&amp;%201%20%5C%5C%0A%20%20%20%20%20%201%20&amp;%200%20%5C%5C%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright)%0A"></p>
<p>And a test to ensure that our code does indeed invert the entries:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb10-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">def</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">00</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:01</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:10</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:11</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>})</span>
<span id="cb10-2"></span>
<span id="cb10-3">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">deftest</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> matrix-mult-test</span></span>
<span id="cb10-4">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> (matrix-mult X {<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>})</span>
<span id="cb10-5">         {<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>})))</span></code></pre></div></div>
<p>An implementation of that using the normal rules of matrix multiplication would be:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb11-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> matrix-mult </span>[A x]</span>
<span id="cb11-2">  {</span>
<span id="cb11-3">    (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:00</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> x)) (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:01</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> x)))</span>
<span id="cb11-4">    (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:10</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:0</span> x)) (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:11</span> A) (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:1</span> x)))</span>
<span id="cb11-5">  })</span></code></pre></div></div>
<p>Now we can reimplement the <code>X</code> function using our matrix:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb12-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> X </span>[qubit]</span>
<span id="cb12-2">  (matrix-mult {<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">00</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:01</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:10</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:11</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>} qubit))</span></code></pre></div></div>
<p>Great. The tests are still passing and we are now in a position to add new gates, for example the very useful <a href="https://en.wikipedia.org/wiki/Quantum_logic_gate#Hadamard_(H)_gate">Hadamard</a> (H) gate that we need for the die. The Hadamard gate takes the qubit to the equator of the Bloch Sphere and, since it’s unitary, applying it twice takes us back to the start. We can capture that in a test<sup>8</sup>:</p>
<div class="no-row-height column-margin column-container"><div id="fn8"><p><sup>8</sup>&nbsp;Strictly speaking this is not a very good test. There are an infinite number of ways to make this it pass but it’s good enough for now.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb13-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">deftest</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> measure-should</span></span>
<span id="cb13-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> (sample <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> #(measure (H (Qubit))))))</span>
<span id="cb13-3">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (sample <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> #(measure (H (H (Qubit))))))))</span></code></pre></div></div>
<p>In this case the matrix for the transformation (still real numbers) is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20=%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D%0A%20%20%20%20%5Cleft(%20%5Cbegin%7Barray%7D%7Bc%7D%0A%20%20%20%20%20%201%20&amp;%201%20%5C%5C%0A%20%20%20%20%20%201%20&amp;%20-1%20%5C%5C%0A%20%20%20%20%5Cend%7Barray%7D%20%5Cright)%0A"></p>
<p>We can implement it, for example, in this way.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb14-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">def</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> </span>^<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:const</span> oosr2 (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> (Math/sqrt <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb14-2">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> H </span>[qubit]</span>
<span id="cb14-3">  (matrix-mult {<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:00</span> oosr2 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:01</span> oosr2 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:10</span> oosr2 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:11</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> oosr2)} qubit))</span></code></pre></div></div>
<p>OK, so we have built our Qubit and a couple of gates, X and H, to manipulate it. With this code we’ve advanced quite a bit and can already simulate a quantum coin toss. In the REPL we can do:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb15-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> coin-toss </span>[] </span>
<span id="cb15-2">  (measure (H (Qubit))))</span>
<span id="cb15-3">=&gt; <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">#'qucl.qubits/coin-toss</span></span>
<span id="cb15-4">(coin-toss)</span>
<span id="cb15-5">=&gt; <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb15-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">repeatedly</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> coin-toss)</span>
<span id="cb15-7">=&gt; (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">false</span>)</span></code></pre></div></div>
<p>Going back to the original feature test, we can also now simulate an 8-sided die simply by tossing a coin 3 times and interpreting the result as binary. We could do this in the REPL:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb16-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> to-int </span>[&amp; b]</span>
<span id="cb16-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reduce</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> [acc v] (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> acc) (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> v <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)))</span>
<span id="cb16-3">          <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb16-4">          b))</span>
<span id="cb16-5">=&gt; <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">#'qucl.qubits/to-int</span></span>
<span id="cb16-6">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">apply</span> to-int (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">repeatedly</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> coin-toss))</span>
<span id="cb16-7">=&gt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span></span>
<span id="cb16-8">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">apply</span> to-int (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">repeatedly</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> coin-toss))</span>
<span id="cb16-9">=&gt; <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span></code></pre></div></div>
<p>Not a bad start but there’s still a lot to do. To implement our feature test completely we need to be able to combine Qubits. We’ve some way to go and the next steps will be to introduce Neanderthal and complex numbers but I’ll leave that for <a href="../2019-05-01-building-a-qpu-simulator-in-clojure-part-2/">next time</a>.</p>
<p><br></p>
<hr>
<section id="appendix" class="level2">
<h2 class="anchored" data-anchor-id="appendix">Appendix</h2>
<p>To make the our feature test compile we can use stub implementations which just throw exceptions, for example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb17-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> to-int </span>[qubits]</span>
<span id="cb17-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">throw</span> (UnsupportedOperationException)))</span></code></pre></div></div>
<p>This isn’t as necessary in Clojure as other JVM based languages because we tend to use the REPL to run just the parts that we need. I don’t have a strong opinions about the best workflow for TDD in Clojure yet, I guess it’s personal and may depend on your preferred tooling.</p>


</section>


 ]]></description>
  <category>quantum-computing</category>
  <category>programming-languages</category>
  <category>software-testing</category>
  <guid>https://johnhearn.github.io/notes/2019-01-25-building-a-qpu-simulator-in-clojure-part-1/</guid>
  <pubDate>Sun, 27 Jan 2019 16:52:00 GMT</pubDate>
</item>
<item>
  <title>Native µservices with Clojure, SparkJava and Graal</title>
  <link>https://johnhearn.github.io/notes/2018-12-24-native-clojure-sparkjava-graal/</link>
  <description><![CDATA[ 




<p>Using Clojure to build serve endpoints is an attractive proposition. Services are naturally functional in nature, in fact a service <em>is</em> a function. We’ll follow the same simple example we used for <a href="../2018-11-12-native-sparkjava-graal/">Java</a> and <a href="../2018-11-14-native-kotlin-sparkjava-graal/">Kotlin</a>. This is a continuation of those articles but please bear in mind that my Clojure skills do not (yet :) match Java and Kotlin.</p>
<p>We start, as you might imagine, with a new project:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">lein</span> new hello-clojure</span></code></pre></div></div>
<p>We need to add the dependencies to the main <code>project.clj</code> file as well as the name of the main class<sup>1</sup> that we’ll run to start the server:</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Something happened with the dash in the name which became an underscore in some places for some reason.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb2-1">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:dependencies</span> [[org.clojure/clojure <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.9.0"</span>]</span>
<span id="cb2-2">                 [com.sparkjava/spark-core <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.7.2"</span>]</span>
<span id="cb2-3">                 [org.slf4j/slf4j-simple <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.7.13"</span>]]</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:main</span> hello_clojure.core)</span></code></pre></div></div>
<p>Clojure is interoperable with Java but not to the same extent that Kotlin is. To overcome the differences I used a couple of adapters from neat clojure code to Spark’s Java classes<sup>2</sup>.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;I later found a nice <a href="https://lispchronicles.com/shortn.html">article</a> with a complete service using Clojure and Spark. Their adapters were slightly better than mine so I’ve incorporated some ideas from that article in what follows.</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb3-1">(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ns</span> hello_clojure.core</span>
<span id="cb3-2">  (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:gen-class</span>)</span>
<span id="cb3-3">  (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:import</span> (spark Spark Response Request Route)))</span>
<span id="cb3-4"></span>
<span id="cb3-5">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> route </span>[handler]</span>
<span id="cb3-6">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reify</span> Route</span>
<span id="cb3-7">    (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">handle</span> [_ ^Request request ^Response response]</span>
<span id="cb3-8">      (handler request response))))</span>
<span id="cb3-9"></span>
<span id="cb3-10">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> get </span>[endpoint routefn]</span>
<span id="cb3-11">  (Spark/get endpoint (route routefn)))</span></code></pre></div></div>
<p>Then we’re ready to create the controller which we do from the main method so that it’s easy to invoke from the command line. Note also that in the above we used the <code>gen-class</code> directive to ensure the main class in the Manifest is correct:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb4-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> -main </span>[]</span>
<span id="cb4-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">get</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/sayHello"</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> [req resp] <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello World!!"</span>)))</span></code></pre></div></div>
<p>To simplify the generation of the service we can build a self-contained jar using Leiningen.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> lein <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">clean</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">lein</span> uberjar</span></code></pre></div></div>
<p>As before, we first check that the service works as normal Java:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> java <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-cp</span> target/hello-clojure-0.1.0-SNAPSHOT-standalone.jar hello_clojure.core</span>
<span id="cb6-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb6-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[Thread-0]</span> INFO org.eclipse.jetty.server.Server <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-</span> Started @1033ms</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> curl <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">localhost:4567/sayHello</span></span>
<span id="cb7-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> World!</span></code></pre></div></div>
<p>Compiling to a native image is as simple as the previous examples with Java and Kotlin.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> native-image <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-cp</span> target/hello-clojure-0.1.0-SNAPSHOT-standalone.jar hello_clojure.core</span>
<span id="cb8-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Build</span> on Server<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pid:</span> 35646, port: 53994<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb8-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[hello_clojure.core:35646]</span>    classlist:   2,704.82 ms</span>
<span id="cb8-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[hello_clojure.core:35646]</span>        <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cap</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">:</span>     909.58 ms</span>
<span id="cb8-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb8-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[hello_clojure.core:35646]</span>        write:     647.23 ms</span>
<span id="cb8-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[hello_clojure.core:35646]</span>      <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">total</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>:  54,900.61 ms</span></code></pre></div></div>
<p>And run it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> ./helloworld_clojure</span>
<span id="cb9-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span></span>
<span id="cb9-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[Thread-2]</span> INFO org.eclipse.jetty.server.Server <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-</span> Started @2ms</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> curl <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">localhost:4567/sayHello</span></span>
<span id="cb10-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> World!</span></code></pre></div></div>
<p>Once again the native binary is roughly 15M but again the start-up time is almost instantaneous. This is a very attractive combination of technologies and worth more investigation.</p>




 ]]></description>
  <category>software-architecture</category>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2018-12-24-native-clojure-sparkjava-graal/</guid>
  <pubDate>Mon, 24 Dec 2018 11:52:00 GMT</pubDate>
</item>
<item>
  <title>Comparing Clojure to Kotlin</title>
  <link>https://johnhearn.github.io/notes/2018-12-16-notes-comparing-clojure-to-kotlin/</link>
  <description><![CDATA[ 




<p>One way to learn a new language is to compare it to one you already know. Here I’m taking a program written in Clojure<sup>1</sup> and comparing it with similar code in Kotlin. The objective is two-fold, firstly to help me learn Clojure and secondly to see the similarities and differences between these two languages. In the following I’ll assume some minimum knowledge of both.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;The code is taken from my colleague <a href="https://codurance.com/publications/author/richard-wild/">Richard Wild</a>’s excellent series of articles on <a href="https://codurance.com/2018/11/02/the-functional-style-part-6/">Functional Programming</a>.</p></div></div><p>To warm up lets compare these two different ways of defining a function in Clojure:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb1-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> greet </span>[<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">name</span>] (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, "</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">name</span>))</span>
<span id="cb1-2"></span>
<span id="cb1-3">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">def</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> greet </span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> [<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">name</span>] (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, "</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">name</span>)))</span></code></pre></div></div>
<p>With the Kotlin equivalent:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">greet</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> name</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">greet</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">String</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The syntax is quite different, of course, and Kotlin requires typed parameters where Clojure is dynamically typed. This can be a blessing and a curse but that’s a discussion for another day. Idiomatic Kotlin would also use built-in string templates instead of concatenation but the similarities nonetheless stand out.</p>
<p>Let’s get into the example. First define the neighbour offsets. In Clojure vector instantiation is quite neat (this is formatted manually to show the pattern).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb3-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">def</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> neighbours</span></span>
<span id="cb3-2">  [[-<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-3">   [-<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]         [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb3-4">   [-<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, -<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb3-5"></span>
<span id="cb3-6">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> neighbours-of </span>[x y]</span>
<span id="cb3-7">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">set</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> [[x-offs y-offs]] [(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> x-offs x) (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> y-offs y)])</span>
<span id="cb3-8">            neighbours)))</span>
<span id="cb3-9"></span>
<span id="cb3-10">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> generate-cell </span>[neighbours y x]</span>
<span id="cb3-11">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">contains?</span> neighbours [x y]) <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span></code></pre></div></div>
<p>In Kotlin to do the same is slightly more verbose<sup>2</sup>. We can use <code>Pair</code>s (tuples) and <code>typealias</code>es here to improve the readability.</p>
<div class="no-row-height column-margin column-container"><div id="fn2"><p><sup>2</sup>&nbsp;But not as verbose a Java :)</p></div></div><div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typealias</span> Cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Pair<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typealias</span> Cells <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Collection<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">var</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">neighbours</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb4-5">        setOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb4-6">              Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>              Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb4-7">              Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span></span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">neighboursOf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb4-10">        neighbours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>xOff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> yOff<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>xOff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> yOff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generateCell</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">neighbours</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Cells</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb4-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>neighbours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>contains<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>Cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)))</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div></div>
<p>The thing to note is the use of the <code>map</code> function in both implementations. The parameters are effectively reversed. We’ll see some implications of this later. Next we’ll see some currying (the focus of the article):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb5-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> generate-line </span>[neighbours width y]</span>
<span id="cb5-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> generate-cell neighbours y)</span>
<span id="cb5-3">       (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> width)))</span>
<span id="cb5-4"></span>
<span id="cb5-5">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> generate-board </span>[dimensions neighbours]</span>
<span id="cb5-6">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mapcat</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> generate-line neighbours (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:w</span>))</span>
<span id="cb5-7">          (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:h</span>))))</span></code></pre></div></div>
<p>And in Kotlin:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generateLine</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">neighbours</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Cells</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">width</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb6-2">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> generateCell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>neighbours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">generateBoard</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">neighbours</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Cells</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb6-5">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> generateLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>neighbours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Things to note: - The <code>partial</code> function in Clojure translates naturally to lambda expressions in Kotlin. - While the <code>map</code> function is the same, <code>mapcat</code> becomes <code>flatMap</code> in Kotlin. - It seemed easier in Kotlin to pass <code>h</code> and <code>w</code> as simple parameters rather that inside map. I wonder what the reasoning behind this is and whether it’s a common style in Clojure. - We can use the range operators directly in Kotlin. I actually prefer <code>range</code> as a function too but it’s less idiomatic in Kotlin.</p>
<p>The next bit is much the same:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb7-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> mine? </span>[cell]</span>
<span id="cb7-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\*</span> cell))</span>
<span id="cb7-3"></span>
<span id="cb7-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> board-for-cell </span>[dimensions y x cell]</span>
<span id="cb7-5">  (generate-board dimensions (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mine? cell) (neighbours-of x y))))</span>
<span id="cb7-6"></span>
<span id="cb7-7">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> boards-for-line </span>[dimensions line y]</span>
<span id="cb7-8">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> board-for-cell dimensions y)</span>
<span id="cb7-9">       (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:w</span>))</span>
<span id="cb7-10">       line))</span></code></pre></div></div>
<p>Compared to the Kotlin version:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">isMine</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cell</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb8-2">        <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'*'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> cell</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boardForCell</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cell</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb8-5">        generateBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isMine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span> neighboursOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> emptyList<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">boardsForLine</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">line</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb8-8">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> boardForCell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> line<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Here we can again see the difference between the <code>map</code> functions mentioned earlier. Clojure allows multiple ranges for the map operation, essentially <code>zip</code>ing them together for you. It’s an interesting idea. Note that Kotlin also <em>requires</em> that <code>boardForCell()</code> return a result where Closure does not, defaulting to <code>nil</code>. Kotlin spurns <code>null</code>s and in any case I prefer this approach.</p>
<p>Now the fun part.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb9-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> sum-up </span>[&amp; <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">vals</span>]</span>
<span id="cb9-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reduce</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">vals</span>))</span>
<span id="cb9-3"></span>
<span id="cb9-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> draw </span>[input-board]</span>
<span id="cb9-5">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> [lines (str/split-lines input-board),</span>
<span id="cb9-6">        dimensions {<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:h</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">count</span> lines), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:w</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">count</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">first</span> lines))}]</span>
<span id="cb9-7">    (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-&gt;&gt;</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mapcat</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> boards-for-line dimensions)</span>
<span id="cb9-8">                 lines</span>
<span id="cb9-9">                 (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:h</span>)))</span>
<span id="cb9-10">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">apply</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> sum-up))))</span></code></pre></div></div>
<p>This combines several concepts that were new to me, <code>-&gt;&gt;</code> and <code>apply</code> together with the tricky (for me) <code>map</code> function and some magic with the varadic arguments. It took a while to understand what was going on here and in fact my first couple of naïve attempts didn’t work at all. Since the aim was to get as close to the Clojure code as possible this is what I finally came up with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sumUp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">vararg</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">vals</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">List</span>&lt;<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Int</span>&gt;<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb10-2">        vals<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>reduce <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inputBoard</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">):</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb10-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>size</span>
<span id="cb10-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>first<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>length</span>
<span id="cb10-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">boards</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> boardsForLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sumUp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(*</span>boards<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>toTypedArray<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb10-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Things to note: - Spreading of varadic arguments (with the <code>*</code> operator) only works for arrays in Kotlin so I had to convert the list to a typed array. In Clojure it seems that it’s very easy to spread varadic arguments and there is a <em>big</em> difference between spreading a list and taking a single <code>List</code> argument although it appears very similar. - It doesn’t help that there are no types on the Clojure version of the <code>sumUp()</code> method. I have found that I lean heavily on strong typing but I’ll struggle on :) - There is something attractive about that Clojure code, as compared to the Kotlin version, that I’m not sure I’m fully appreciating yet. However if we don’t make the <code>sumUp</code> function variadic (and make it an extension function instead) then the implementation becomes simpler and matches the Clojure version a little more closely.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sumUp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb11-2">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>reduce <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inputBoard</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">):</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb11-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>size</span>
<span id="cb11-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>first<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>length</span>
<span id="cb11-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> boardsForLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-9">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sumUp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span></code></pre></div></div>
<p>The rest of the exercise consists of manipulating and merging the output to give the final result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb12-1">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> cell-as-text </span>[cell-value]</span>
<span id="cb12-2">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">zero?</span> cell-value) <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\space</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span> cell-value)))</span>
<span id="cb12-3"></span>
<span id="cb12-4">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> overlay-cell </span>[top bottom]</span>
<span id="cb12-5">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (mine? top) top bottom))</span>
<span id="cb12-6"></span>
<span id="cb12-7">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> overlay-boards </span>[top bottom]</span>
<span id="cb12-8">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reduce</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> overlay-cell top bottom)))</span>
<span id="cb12-9"></span>
<span id="cb12-10">(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">defn</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"> draw </span>[input-board]</span>
<span id="cb12-11">  (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> [lines (str/split-lines input-board),</span>
<span id="cb12-12">        dimensions {<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:h</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">count</span> lines), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:w</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">count</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">first</span> lines))}]</span>
<span id="cb12-13">    (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-&gt;&gt;</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mapcat</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> boards-for-line dimensions)</span>
<span id="cb12-14">                 lines</span>
<span id="cb12-15">                 (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">range</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:h</span>)))</span>
<span id="cb12-16">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">apply</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> sum-up)</span>
<span id="cb12-17">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> cell-as-text)</span>
<span id="cb12-18">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partition</span> (dimensions <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">:w</span>))</span>
<span id="cb12-19">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">map</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">partial</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reduce</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span>))</span>
<span id="cb12-20">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">interpose</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\newline</span>)</span>
<span id="cb12-21">         (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reduce</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span>)</span>
<span id="cb12-22">         (overlay-boards input-board))))</span></code></pre></div></div>
<p>Translated directly to Kotlin’s built-in functions it looks something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cellAsText</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">cellValue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb13-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cellValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> cellValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>toString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overlayCell</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">top</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bottom</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb13-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isMine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>top<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span> top <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> bottom</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inputBoard</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">):</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb13-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>size</span>
<span id="cb13-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>first<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>length</span>
<span id="cb13-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> sumUp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">((</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> boardsForLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">})</span></span>
<span id="cb13-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(::</span>cellAsText<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chunked<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-14">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>joinToString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb13-15">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>joinToString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-17">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> overlayCell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb13-18">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>joinToString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-19"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Here Clojure’s <code>partition</code> function is replaced by Kotlin’s <code>chunked</code> function and <code>str</code> with string functions like <code>joinToString</code>. Once again Clojure’s multi-argument <code>map</code> function does not have a direct translation in Kotlin so we use <code>zip</code> to merge the result with the initial board to generate the final results. To get something more closely resembling thr Clojure code we can take advantage of Kotlin’s extension methods. We’ll need to create some helper methods.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> listOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>joinToString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">List</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">interpose</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb14-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> listOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>sep<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}.</span>drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>An then use them to build the data pipeline:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">overlayBoards</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">other</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb15-2">        other<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>zip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">this</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>overlayCell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>joinToString<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inputBoard</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CharSequence</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">):</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lines</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">h</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>size</span>
<span id="cb15-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">val</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">w</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>first<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">().</span>length</span>
<span id="cb15-8"></span>
<span id="cb15-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> until h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span>flatMap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> boardsForLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> lines<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-10">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sumUp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb15-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(::</span>cellAsText<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chunked<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>reduce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(::</span>str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-14">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>interpose<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-15">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>reduce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(::</span>str<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>overlayBoards<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>inputBoard<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Using extension methods has allowed us to put the other functions like <code>interpose</code> and <code>overlayBoards</code> into the pipeline giving us an effect similar to Clojure’s thread-last operator.</p>
<p>We can check the output by calling the <code>draw</code> function from <code>main</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode kotlin code-with-copy"><code class="sourceCode kotlin"><span id="cb16-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fun</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">main</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">args</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Array</span>&lt;<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">String</span>&gt;<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-2">    println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>draw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*   </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> *  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   *"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span></span>
<span id="cb16-3"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And it does indeed, in case you’re wondering, give us the results we expect.</p>
<pre><code>*21 
2*21
12*2
 12*</code></pre>
<section id="conclusion" class="level3">
<h3 class="anchored" data-anchor-id="conclusion">Conclusion</h3>
<p>I was impressed by Clojure’s ability to simplify complex pipelines using the thread-last <code>-&gt;&gt;</code> operator. I needed to resort to extension methods to get the same effect in Kotlin. It’s a powerful thing and now I’m wondering why I haven’t seen it before. Maybe it’s only possible because of Clojure’s dynamic typing. The way the <code>map</code> operation is implemented is also very useful as compared to similar approaches in other languages.</p>
<p>I’m starting to get the gist of Clojure’s syntax (if only scratching the surface of it runtime). This exercise has helped really understand some non-trivial Clojure code. Next step will be to convert some existing functional style Kotlin to Clojure. That’s for another day.</p>


</section>


 ]]></description>
  <category>programming-languages</category>
  <guid>https://johnhearn.github.io/notes/2018-12-16-notes-comparing-clojure-to-kotlin/</guid>
  <pubDate>Sun, 16 Dec 2018 17:22:00 GMT</pubDate>
</item>
</channel>
</rss>
