<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by gyu-don on Medium]]></title>
        <description><![CDATA[Stories by gyu-don on Medium]]></description>
        <link>https://medium.com/@beef_and_rice?source=rss-dae71332fc30------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*J15tC6nyFCdDrudKcdLrJA.jpeg</url>
            <title>Stories by gyu-don on Medium</title>
            <link>https://medium.com/@beef_and_rice?source=rss-dae71332fc30------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 15:24:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@beef_and_rice/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Quantum Phase Estimation is Not Difficult]]></title>
            <link>https://medium.com/@beef_and_rice/quantum-phase-estimation-is-not-difficult-a03252dc2bf0?source=rss-dae71332fc30------2</link>
            <guid isPermaLink="false">https://medium.com/p/a03252dc2bf0</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[quantum]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[quantum-computer]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[gyu-don]]></dc:creator>
            <pubDate>Thu, 26 May 2022 14:42:59 GMT</pubDate>
            <atom:updated>2022-05-26T14:48:39.757Z</atom:updated>
            <content:encoded><![CDATA[<p>Quantum Phase Estimation (QPE) is one of the most crucial quantum algorithms. When you have the quantum circuit <em>A</em> such that <em>A|0&gt; = |ψ&gt; </em>where <em>|ψ&gt;</em> is an eigenvector of the unitary matrix <em>U</em>, you can get the corresponding eigenvalue <em>λ</em> as a digital fixed-point number. You have probably seen the following QPE circuit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*PTRgQRNkzwJz1RosmXR6mw.png" /></figure><p>But do you understand the meaning of this circuit? If not, today, you know it.</p><p>First, unitary matrices are diagonalizable, so <em>U</em> can be diagonalized as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/1*dMjn5OiJTMusHIRWsFyQsQ.png" /></figure><p>Without loss of generality, we can assume that <em>|ψ&gt;</em> is the most left column of <em>P</em>. So, in this case, these circuits are the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*an5Q8wuT45Y_Ec-3NImRXg.png" /></figure><p>Of course, we can decompose Controlled-U circuits and cancel <em>P</em>⁻¹<em> P</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*GjWmsTSDJ1srXDPeQBOrAQ.png" /></figure><p>It’s very simplified. But we can simplify it more.</p><p>The target qubits of Controlled-Λ gates are <em>|0&gt;|0&gt;…|0&gt;</em>. So, if the control qubit is |1&gt;, the corresponding eigenvalue <em>λ</em> is kicked back into the control qubit. So, we can rewrite them to phase-shift gates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*n-uCtSLKWgKcKxGSqBcirg.png" /></figure><p>The definition of the phase-shift gate P(θ) is as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/147/1*focjP4ghEcxpxLKjDoMTcA.png" /></figure><p>Cool, we got a simplified QPE circuit, but why does this circuit return a fixed-point number of <em>λ</em>?</p><p>I tell the truth. Applying Hadamard gates into <em>|0&gt;</em> is regarded as operating QFT into <em>|0&gt;</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*lF8P7lSFfq8WPu7Jp78qyA.png" /></figure><p>So, now, our circuit is as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*uFXGueTllibLpEF9LXt1kQ.png" /></figure><p>Do you remember the formula for the Fourier transform? The rotation in the frequency domain is the shifting in the time domain. What I mean is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*Kbl1Ka7h0jTqpKCkWXjSjQ.png" /></figure><p>Finally, we get the time-shifting, <em>0</em> to <em>λ</em>. It is what QPE means.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a03252dc2bf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get Started VQE with Blueqat]]></title>
            <link>https://medium.com/@beef_and_rice/get-started-vqe-with-blueqat-2ef6a73bbaee?source=rss-dae71332fc30------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ef6a73bbaee</guid>
            <category><![CDATA[blueqat]]></category>
            <category><![CDATA[quantum-computer]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <dc:creator><![CDATA[gyu-don]]></dc:creator>
            <pubDate>Thu, 13 Feb 2020 02:00:50 GMT</pubDate>
            <atom:updated>2020-02-13T02:00:50.697Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/Blueqat/Blueqat">Blueqat</a>, the quantum computing library assists to write variational quantum eigensolver (VQE) algorithm.</p><p>This article explains how to write VQE for 2x2 Hamiltonian in Blueqat.</p><p>If you want to know the overview of VQE, <a href="https://developer.ibm.com/dwblog/2017/quantum-computing-qubit-vqe-variational-quantum-eigensolver/">this article</a> is prefer.</p><h3>All you need is Hamiltonian and Ansatz</h3><p>First, you have to make a Hamiltonian, it is the problem to solve with VQE.<br> Second, you have to prepare “ansatz”, it is parametrized quantum circuit.</p><p>Blueqat prepares some built in ansatzes like QAOA ansatz. However, sometimes, it is not enough to solve your Hamiltonian.</p><p>In this article, we define new ansatz for 1 qubit (2x2 Hamiltonian) VQE.</p><h3>blueqat.pauli helps to make the Hamiltonian</h3><p>Making Hamiltonian by Pauli matrices is very easy.</p><pre>from blueqat.pauli import X, Y, Z, I</pre><pre>h = 1.23 * I - 4.56 * X(0) + 2.45 * Y(0) + 2.34 * Z(0)</pre><h3>Making randomized Hamiltonian</h3><p>We make randomized Hamiltonian.</p><pre>from random import random<br>from blueqat.pauli import X, Y, Z, I</pre><pre>h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)</pre><p>Now, h is a Hamiltonian with random matrix coefficient.</p><h3>Ansatz is a class which inherits blueqat.vqe.AnsatzBase</h3><p>Ansatz is a class which inherits blueqat.vqe.AnsatzBase and is implemented get_circuit() method.</p><p>To make a arbitrary 1 qubit gate, apply a RY gate and a RZ gate is enough.<br> Let’s make the ansatz.</p><pre>from blueqat import Circuit<br>from blueqat.vqe import AnsatzBase</pre><pre>class OneQubitAnsatz(AnsatzBase):<br>    def __init__(self, hamiltonian):<br>        AnsatzBase.__init__(hamiltonian, 2) # Hamiltonian and the number of parameters</pre><pre>    def get_circuit(self, params):<br>        # Return a circuit which applied a RY gate and a RZ gate.<br>        a, b = params<br>        return Circuit().ry(a)[0].rz(b)[0]</pre><p>Very simple.</p><h3>VQE works with a Hamiltonian and an ansatz</h3><pre>from blueqat.vqe import Vqe</pre><pre>h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)<br>runner = Vqe(OneQubitAnsatz(h))<br>result = runner.run()</pre><pre>print(&#39;Result by VQE&#39;)<br>print(runner.ansatz.get_energy(result.circuit, runner.sampler))</pre><p>runner = Vqe(OneQubitAnsatz(h)): Make a runner to run the VQE with the ansatz.<br> result = runner.run(): Run the VQE and get the result.</p><p>runner.ansatz.get_energy(result.circuit, runner.sampler): Get the energy from result.<br> This is the most complicated line in this article. Copy and paste is your friend!</p><p>Now, you obtained the energy (smallest eigenvalue of the Hamiltonian matrix).</p><h3>Compare the result with numpy’s solution</h3><p>Is our result correct answer? Let’s compare with numpy’s solution.</p><p>blueqat.pauli can transform Hamiltonian to numpy&#39;s array by to_matrix() method.<br> We can get the eigenvalue by np.linalg.eigh.</p><pre>import numpy as np<br># Hamiltonian to matrix<br>mat = h.to_matrix()<br># Calculate by numpy<br>print(&#39;Result by numpy&#39;)<br>print(np.linalg.eigh(mat))</pre><h3>Run from the beginning to the end</h3><p>Whole source code is as following:</p><pre>from random import random<br>import numpy as np<br>from blueqat import Circuit<br>from blueqat.pauli import X, Y, Z, I<br>from blueqat.vqe import AnsatzBase, Vqe</pre><pre>class OneQubitAnsatz(AnsatzBase):<br>    def __init__(self, hamiltonian):<br>        super().__init__(hamiltonian, 2)</pre><pre>    def get_circuit(self, params):<br>        a, b = params<br>        return Circuit().ry(a)[0].rz(b)[0]<br></pre><pre># Calculate by VQE<br>h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)<br>runner = Vqe(OneQubitAnsatz(h))<br>result = runner.run()</pre><pre>print(&#39;Result by VQE&#39;)<br>print(runner.ansatz.get_energy(result.circuit, runner.sampler))</pre><pre># Hamiltonian to matrix<br>mat = h.to_matrix()<br># Calculate by numpy<br>print(&#39;Result by numpy&#39;)<br>print(np.linalg.eigh(mat)[0][0])</pre><p>This is the example of result.<br> You can see VQE result and numpy result are almost same value.</p><pre>Result by VQE<br>-0.7072131424381115<br>Result by numpy<br>-0.7072139372798669</pre><h3>Summary</h3><p><a href="https://github.com/Blueqat/Blueqat">Blueqat</a> library can write the VQE easily.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ef6a73bbaee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Important Facts about Quantum Computer for Investors]]></title>
            <link>https://medium.com/mdr-inc/important-facts-about-quantum-computer-for-investors-bb0a93aee489?source=rss-dae71332fc30------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb0a93aee489</guid>
            <category><![CDATA[quantum-computing]]></category>
            <dc:creator><![CDATA[gyu-don]]></dc:creator>
            <pubDate>Wed, 30 Oct 2019 03:58:58 GMT</pubDate>
            <atom:updated>2019-10-30T04:11:23.746Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SGGaJkesCzRzhHms8tyyIg.png" /></figure><p>I’m quantum computing engineer of MDR Inc. and I often exhibit our company booth.<br>Many investors came and they asked almost same questions.<br>Therefore, I summarize some important facts.</p><h3>Today’s quantum computer and Future’s quantum computer</h3><p>They’re very different.</p><h4>Today: “noisy”</h4><p>Due to environmental noise, so many calculation error is occurred.<br>This is critical problem. Complicated calculations are almost wrong and simple calculations are easy to calculate without quantum computers.<br>NOISY QUANTUM COMPUTER CANNOT BREAK CRYPTOGRAPHY!!<br>(Your bitcoin is still safe)</p><h4>Future: “fault-tolerant”</h4><p>If so many qubits are available, error correction is possible.</p><p><strong>“So many” is how many?<br></strong>Depends on error rate.<br>It’s only an estimation, but, approx 1,000,000 qubits are required for useful fault-tolerant quantum computer. (Details are <a href="https://www.enterpriseai.news/2018/04/27/quantum-error-correction-googles-strategy-for-qubit-accuracy/">here</a>.)<br>Please note that today’s Google and IBM’s latest quantum computers are 53 qubits.</p><p><strong>When is it appear?<br></strong>I don’t know. Maybe, 30 years later.</p><h3>What kind of applications are expected with quantum computer?</h3><p>In today’s quantum computer, there’s no practical applications.<br>Of course, I know Google’s quantum supremacy news. It’s great achievement. However, there’s no practical applications today.</p><p>COMMERCIAL USE OF QUANTUM COMPUTER IS NO ADVANTAGE.<br>YOU SHOULD USE NORMAL COMPUTER.</p><p>Following applications are expected for quantum speedup (in the future).</p><ul><li>Quantum chemistry<br> Scientific research<br> Material developing<br> Pharmaceutical</li><li>Machine learning<br>Boltzmann machine and Born machine<br>Generative model</li><li>Optimization<br>Scheduling<br>Recommendation engine</li><li>Fintech<br>Portfolio optimization<br>Monte-Carlo simulation</li><li>Breaking cryptography<br>RSA, ECDSA will be broken.</li></ul><p>We think, <a href="https://en.wikipedia.org/wiki/Generative_model">generative model</a> will be useful in the near.</p><h4>FAQ</h4><h4>Will the quantum computer replace all traditional computers?</h4><p>No. The quantum computer is expected to speed up for large scale problems.<br>For general purpose likes web browsing, excel or e-mail, quantum computer has no advantage.</p><h4>How much times does the quantum computer faster than the traditional computer?</h4><p>“xx times faster” is misleading.</p><p>It depends on “quantum algorithm” and scale of problem. Using better algorithm is the most effective way to speedup the software.<br>The quantum computer extends the variations of algorithm. (a.k.a. “Quantum algorithm”)<br>If quantum algorithm is more effective than non-quantum algorithm, you can get quantum speedup.</p><h4>What is the business model of MDR Inc.?</h4><p>We’re full-stack quantum computer company.</p><p>We develop applications, library/middleware, hardware also. We will make platform for quantum computing. Provides applications and programming API for end-users and engineers.</p><p>Blueqat library is the easiest library to write quantum computing application. And Blueqat can run many “backends” for example, CPU, GPU, cloud, IBM Q. We can add new backends for other quantum computers or simulators. IBM qiskit also has “backend” system, however, they doesn’t want to connect with third-party simulators or quantum computers.</p><p>We aspect, quantum computing library shall runnable on many systems.</p><h4>What is round of MDR Inc.?</h4><p>2018, October: 1.83 Million USD from SBI Investment (Series A)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb0a93aee489" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mdr-inc/important-facts-about-quantum-computer-for-investors-bb0a93aee489">Important Facts about Quantum Computer for Investors</a> was originally published in <a href="https://medium.com/mdr-inc">Blueqat (blueqat Inc. / former MDR Inc.)</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Python’s __getitem__ and slicing]]></title>
            <link>https://medium.com/@beef_and_rice/mastering-pythons-getitem-and-slicing-c94f85415e1c?source=rss-dae71332fc30------2</link>
            <guid isPermaLink="false">https://medium.com/p/c94f85415e1c</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[gyu-don]]></dc:creator>
            <pubDate>Fri, 17 May 2019 06:25:54 GMT</pubDate>
            <atom:updated>2019-05-17T06:25:54.304Z</atom:updated>
            <content:encoded><![CDATA[<p>In Python’s data model, when you write foo[i] , foo.__getitem__(i) is called. You can implement __getitem__ method in your class, and define the behaviour of slicing.</p><p>Example:</p><pre>class Foo:<br>    def __getitem__(self, key):<br>        print(key)<br>        return None</pre><pre>foo = Foo()<br>foo[1] # =&gt; print(1)<br>foo[&quot;aaa&quot;] # =&gt; print(&quot;aaa&quot;)</pre><p>As you know, Python can slice more complicated object.</p><pre># comma separated slicing<br>d = {}<br>d[1, &quot;hello&quot;] = 3</pre><pre># colon separated slicing<br>ls = [0, 1, 2, 3]<br>ls[:2]<br>ls[3:]<br>ls[2:4]<br>ls[::2]<br>ls[1:4:-1]<br>ls[:]<br>ls[::]</pre><pre># both comma and colon<br>import numpy as np<br>a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<br>a[:2, 1:3]</pre><p>Let’s inspect what kind of object is passed to __getitem__ argument.</p><p>Define the class for inspection.</p><pre>class Inspector:<br>    def __getitem__(self, key):<br>        return key</pre><h3>Comma Separated Slicing</h3><p>First, we check comma separated slicing.</p><pre>a = Inspector()<br>a[1] # =&gt; 1: int</pre><pre>a[1, 2] # =&gt; (1, 2): tuple</pre><pre>a[1, 2, 3] # =&gt; (1, 2, 3): tuple</pre><pre>a[] # =&gt; SyntaxError</pre><p>This behaviour is very similar to right hand side of assignment expression.</p><pre>t = 1 # 1: int<br>t = 1, 2 # (1, 2): tuple<br>t = 1, 2, 3 # (1, 2, 3): tuple<br>t = # SyntaxError</pre><p>More examples needed?</p><pre>a[(1, 2, 3)] # (1, 2, 3): tuple<br>assert a[(1, 2, 3)] == a[1, 2, 3]<br>t = (1, 2, 3)<br>u = 1, 2, 3<br>assert t == u</pre><pre># tuple of one element.<br>a[1,] # (1,): tuple<br>t = 1, # (1,): tuple<br>a[(1,)] # (1,): tuple<br>t = (1,) # (1,): tuple</pre><pre># empty tuple<br>a[()] # (): tuple<br>t = () # (): tuple</pre><pre>t = 1, 2<br>assert a[t] == a[1, 2]</pre><pre>a[(1, 2), 3] # ((1, 2), 3): tuple<br>t = (1, 2), 3 # ((1, 2), 3): tuple</pre><p>However, starred item and yield expression are not allowed for slicing.</p><pre>t = *iter([]), 1 # (1,): tuple<br>a[*iter([]), 1] # SyntaxError: invalid syntax</pre><pre>def g():<br>    t = yield 1 # Valid.<br>    a = Inspector()<br>    a[yield 1] # SyntaxError: invalid syntax</pre><h4>Conclusion of comma separated slice</h4><p>As same as right hand side of assignment expression, comma separated slice is interpreted as tuple.</p><h3>Colon separated slice</h3><p>Let us inspect colon separated slice.</p><pre># As same as above part.<br>class Inspector:<br>    def __getitem__(self, key):<br>        return key</pre><pre>a = Inspector()</pre><p>Colon separated slice makes a slice object.</p><pre>a[1:2:3] # =&gt; slice(1, 2, 3)<br>assert a[1:2:3] == slice(1, 2, 3)</pre><p>Docstring of slice object is:</p><pre>slice(stop)<br>slice(start, stop[, step])</pre><pre>Create a slice object.  This is used for extended slicing (e.g. a[0:10:2]).</pre><p>Colon separated slice’s notifications are [start:], [start::], [:stop] , [:stop:, [::step], [start:stop], [start:stop:], [start::step] , [:stop:step] and [start:stop:step] .</p><pre>start = 1<br>stop = 2<br>step = 3</pre><pre>assert a[start:] == a[start::] == slice(start, None, None) == slice(start, None)</pre><pre>assert a[:stop] == a[:stop:] == slice(None, stop, None) == slice(stop)</pre><pre>assert a[start:stop] == a[start:stop:] == slice(start, stop, None) == slice(start, stop)</pre><pre>assert a[:stop:step] == slice(None, stop, step)</pre><pre>assert a[start:stop:step] == slice(start, stop, step)</pre><p>Now, you understood relations between colon separated slicing and slice object.</p><p>Type of start, stop, step are not only integer. Any types are acceptable.</p><pre>a[[]:{}:&quot;abc&quot;] # =&gt; slice([], {}, &quot;abc&quot;)</pre><pre>class MyClass:<br>    pass</pre><pre>a[MyClass():MyClass()] # =&gt; slice(MyClass(), MyClass())</pre><h4>Handling of slice object</h4><p>slice object have start, stop, and step properties.</p><pre>sl = slice(1, &#39;2&#39;, [3])</pre><pre>sl.start # =&gt; 1<br>sl.stop # =&gt; &#39;2&#39;<br>sl.step # =&gt; [3]</pre><pre>sl = slice(&#39;stop&#39;)</pre><pre>sl.start # =&gt; None<br>sl.stop # =&gt; &#39;stop&#39;<br>sl.step # =&gt; None</pre><p>slice object have indices(len) method.</p><p>Docstring is:</p><pre>S.indices(len) -&gt; (start, stop, stride)</pre><pre>Assuming a sequence of length len, calculate the start and stop<br>indices, and the stride length of the extended slice described by<br>S. Out of bounds indices are clipped in a manner consistent with the<br>handling of normal slices.</pre><p>indices method returns if start , stop , step properties are not None and in the range of [0, len], returns them, otherwise, returns suitable value.</p><p>It helps to implement the slicing as same as list object.</p><pre>li = [i for i in range(10)]<br>def make_list(start, stop, stride):<br>    t = []<br>    if stride &gt; 0:<br>        while start &lt; stop:<br>            t.append(start)<br>            start += stride<br>    else:<br>        while start &gt; stop:<br>            t.append(start)<br>            start += stride<br>    return t</pre><pre>start, stop, stride = a[6:].indices(10)<br># start, stop, stride = 6, 10, 1<br>assert li[6:] == make_list(start, stop, stride) == [6, 7, 8, 9]</pre><pre>start, stop, stride = a[:7:2].indices(10)<br># start, stop, stride = 0, 7, 2<br>assert li[:7:2] == make_list(start, stop, stride) == [0, 2, 4, 6]</pre><pre>start, stop, stride = a[:].indices(10)<br># start, stop, stride = 0, 10, 1<br>assert li[:] == make_list(start, stop, stride) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre><pre>start, stop, stride = a[15:].indices(10)<br># start, stop, stride = 10, 10, 1<br>assert li[15:] == make_list(start, stop, stride) == []</pre><pre>start, stop, stride = a[-3:].indices(10)<br># start, stop, stride = 7, 10, 1<br>assert li[-3:] == make_list(start, stop, stride) == [7, 8, 9]</pre><pre>start, stop, stride = a[:5:-2].indices(10)<br># start, stop, stride = 9, 5, 2<br>assert li[:5:-2] == make_list(start, stop, stride) == [9, 7]</pre><h4>Conclusion of colon separated slicing</h4><p>Colon separated slicing returns slice object. It has 3 properties. start, stop and step. If you want to implement the slicing as same as list object’s implementation, slice.indices method is helpful.</p><h3>Both comma and colon separated slicing</h3><p>In this case, it is interpreted as tuple of slice.</p><pre>class Inspector:<br>    def __getitem__(self, key):<br>        return key</pre><pre>a = Inspector()</pre><pre>a[1:2,3:4] # (slice(1, 2, None), slice(3, 4, None))<br>a[1,2:3,4] # (1, slice(2, 3, None), 4)<br>a[1:(2,3):4] # slice(1, (2, 3), 4)</pre><h3>__index__ method</h3><p>__index__ method is very important to be a slicing master.</p><pre>import numpy as np</pre><pre>i = np.int32(3)<br>print(i) # =&gt; &#39;3&#39;<br>print(type(i)) # =&gt; &#39;np.int32&#39;</pre><p>In this code, is ia integer?</p><p>Yes. It is an integer type object but not a Python’s builtin int type object.</p><p>It may cause some troubles in your __getitem__ implementation. You may want to convert to Python’s builtin int type.</p><p>You may think int(i) returns builtin int type. Yes, it is commonly used in Python. However, if i is floating point number, you may not want to convert to integer value. In this case, __index__ method is useful.</p><blockquote>object.__index__(self)<br>Called to implement operator.index(), and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in bin(), hex() and oct() functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer.</blockquote><blockquote>Note<br>In order to have a coherent integer type class, when __index__() is defined __int__() should also be defined, and both should return the same value.<br>(Python documentation <a href="https://docs.python.org/3/reference/datamodel.html#object.__index__">Data model</a>)</blockquote><p>numpy.int{8, 16, 32, 64}, uint{8, 16, 32, 64} implements __index__ method and it returns Python’s builtin int type. float , numpy’s floating point numbers are not implements __index__ method.</p><p>When you want to convert an variable to int type for slicing, you should use this method.</p><p>You can also use operator.index function. When __index__ is not implemented foo.__index__() raises AttributeError, operator.index(foo) raises TypeError .</p><pre>import operator<br>import numpy as np</pre><pre>i = np.int32(3)<br>i.__index__() # Returns 3: int<br>operator.index(i) # Returns 3: int</pre><pre># builtins int type also implements __index__ method.<br>i = 3<br>i.__index__() # Returns 3: int<br>operator.index(i) # Returns 3: int</pre><pre># floating point type doesn&#39;t implement __index__ method.<br>i = 3.0<br>i.__index__() # AttributeError: &#39;float&#39; object has no attribute &#39;__index__&#39;<br>operator.index(i) # TypeError: &#39;float&#39; object cannot be interpreted as an integer</pre><h3>__setitem__ and __delitem__</h3><p>__setitem__ and __delitem__ are similar method with __getitem__ .</p><p>foo.__setitem__(key, value) is called when foo[key] = value is evaluated. foo.__delitem__(key) is called when del foo[key] is evaluated. You may implement these methods for your class.</p><h3>Conclusion</h3><p>In this article, we learned following.</p><ul><li>foo[key] calls foo.__getitem__(key)</li><li>foo[key] = value calls foo.__setitem__(key, value)</li><li>del foo[key] calls foo.__delitem__(key)</li><li>foo[k1, k2] calls foo.__getitem__((k1, k2))</li><li>foo[k1:k2:k3] calls foo.__getitem__(slice(k1, k2, k3))</li><li>Integer type implements __index__ method</li></ul><p>This knowledge is useful for implementation of your subscriptable class.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c94f85415e1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding how to create Toffoli gate using Blueqat]]></title>
            <link>https://medium.com/@beef_and_rice/understanding-how-to-create-toffoli-gate-using-blueqat-1d313c067730?source=rss-dae71332fc30------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d313c067730</guid>
            <category><![CDATA[blueqat]]></category>
            <category><![CDATA[quantum-computer]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <dc:creator><![CDATA[gyu-don]]></dc:creator>
            <pubDate>Wed, 15 May 2019 08:20:52 GMT</pubDate>
            <atom:updated>2019-05-16T07:45:59.695Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Blueqat?</h3><p>Blueqat is an open source library for quantum computer.</p><p><a href="https://github.com/Blueqat/Blueqat">Blueqat/Blueqat</a></p><h4>How to install Blueqat?</h4><p>Blueqat is provided as Python (ver 3.6+) library. You can install by pip.</p><p>pip install blueqat</p><h3>What is Toffoli gate?</h3><p>Toffoli gate is 2-controlled NOT gate. If both 2 control qubits are |1&gt;, Toffoli gate flip a target qubit (|0&gt; → |1&gt;, |1&gt; →|0&gt;), otherwise do nothing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1bad940f4de349dab0080b55ba085e4/href">https://medium.com/media/e1bad940f4de349dab0080b55ba085e4/href</a></iframe><h3>Toffoli gate implementation of IBM OpenQASM</h3><p>OpenQASM’s standard library qelib1.inc defines the Toffoli gate. Here is its definition.</p><pre>gate ccx a,b,c<br>{<br>  h c;<br>  cx b,c; tdg c;<br>  cx a,c; t c;<br>  cx b,c; tdg c;<br>  cx a,c; t b; t c; h c;<br>  cx a,b; t a; tdg b;<br>  cx a,b;<br>}</pre><p>“h” is a Hadamard gate, “cx” is a CNOT gate, “t” is a T gate which rotates the Bloch sphere Z-axis by π/4 radians, and “tdg” is T† gate which rotates the Bloch spere Z-axis by -π/4 radians.</p><p>To understand this implementation, we reorder some operations. Generally, gate operations are not commutable. However, some gates can be commutable for example single qubit operation for i-th qubit and for j-th qubit, if i ≠ j, they’re commutable. cx a, b and t a are also commutable because T gate rotate only Z-axis and CNOT gate don’t minds control qubit’s phase.</p><p>Here is reordered implementation.</p><pre>gate ccx a,b,c<br>{<br>  h c;<br>  cx b,c; tdg c;<br>  cx a,c; t c;<br>  cx b,c; tdg c;<br>  cx a,c; t c;<br>  h c;</pre><pre>  t a; t b;<br>  cx a,b; tdg b; cx a,b;<br>}</pre><p>This implementation can be separate to 2 parts. First part is making ccx except phase adjustment, second part is adjusting phase factors.</p><h3>Create Toffoli gate</h3><p>First part, some operations are sandwiched with Hadamard gates.</p><pre>  h c;<br>  cx b,c; tdg c;<br>  cx a,c; t c;<br>  cx b,c; tdg c;<br>  cx a,c; t c;<br>  h c</pre><p>To begin from conclusion, 2-controlled Z gate is sandwiched with H gates.</p><p>As we know, -H-Z-H- gate is X gate. We can confirm it with Blueqat.</p><pre>from blueqat import Circuit</pre><pre>print(Circuit().h[0].z[0].h[0].to_unitary())<br># =&gt; Matrix([[0, 1], [1, 0]]), This is X gate.</pre><pre># We can see X gate definition by<br>print(Circuit().x[0].to_unitary())<br># =&gt; Matrix([[0, 1], [1, 0]]), Yes! They&#39;re same!</pre><p>If you’re using Jupyter Notebook, you can get more pretty display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*Hwr5VRdil2iljsJe6_tyUA.png" /></figure><p>Controlled Z gate sandwiched with H gates for target qubit is CNOT gate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*gxzGxoEUq0t1DduYeykn-Q.png" /></figure><p>As same as these, 2-controlled Z gate sandwiched with H gates is Toffoli gate. However, in this part, we will get strange 2-controlled Z gate because adjustment of phase factor is not yet.</p><p>Let us implement 2-controlled Z gate using Blueqat.</p><pre>cx b,c; tdg c;<br>cx a,c; t c;<br>cx b,c; tdg c;<br>cx a,c; t c;</pre><p>We choose a = 2, b = 1, c = 0.</p><p>Implementation is very easy.</p><pre>c = Circuit()<br>c.cx[1, 0].tdg[0]<br>c.cx[2, 0].t[0]<br>c.cx[1, 0].tdg[0]<br>c.cx[2, 0].t[0]<br>c.to_unitary()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*fuUVMIeSz2AiPY0v3ENNzw.png" /></figure><p>Phase factor is strange, but similar to 2-controlled Z gate.</p><p>Add H gates before first opearation and after last operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*nEeD50_Q1VN2uBU9BwVx9w.png" /></figure><p>Is it a Toffoli gate? It seems not a Toffoli gate. But if we know, exp(-iπ/4) = i * exp(iπ/4), we can feel it is similar to Toffoli gate.</p><p>Blueqat’s unitary gate is Sympy matrix and Sympy can simplify these terms automatically. Let’s simplify the unitary matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*VOZ6WTyFD414MfQ5RydyEw.png" /></figure><p>It is as same as Toffoli gate except phase factors.</p><p>Then, we will see second part.</p><pre> t a; t b;<br> cx a,b; tdg b; cx a,b;</pre><p>What is this means? Check by Blueqat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*0hoJfvClRBRK82DxCzrrDA.png" /></figure><p>Wow! This part multiples phase factor i if both qubit 1 and qubit 2 are |1&gt;.</p><p>Finally, we combine first part and second part.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*D9vKx1NjdiqtY6vXNXsAYw.png" /></figure><p>Is something wrong? Don’t worry. First, exp(3iπ/8) = (-1)^3/8 because -1 =exp(iπ) and then, we can ignore scalar part of unitary matrix, it is “global phase”.</p><p>So, divide by [0, 0] element and simplify the matrix, we can get Toffoli gate matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*RbQMQnIzUfZH00-GVVJ7cw.png" /></figure><p>Perfect!</p><h3>Learn Blueqat more!</h3><p>I’m a core developer of Blueqat. Blueqat has very simple and easy interface for quantum computing. I recommend to use Blueqat for learning quantum computer programming.</p><p>We prepared Blueqat tutorials and we will add more and more contents.</p><p><a href="https://github.com/Blueqat/Blueqat-tutorials">Blueqat/Blueqat-tutorials</a></p><p>If you have any questions or suggestions, please feel free to contact us by our <a href="https://mdrqcglobal.slack.com/">Slack community</a>. (Join from <a href="https://join.slack.com/t/mdrqcglobal/shared_invite/enQtNjM1NDgxMjg2NDY4LWJmNjU1Mzk1OTcwN2IwZTY3ZjUyOTM3ZDZjNjJiMzkwZTc2ZmQ0OTY4YTgyN2JmZTQzY2VjMmIxMTk1NTE0YmM">here</a>)</p><p>And Blueqat repository is here.</p><p><a href="https://github.com/Blueqat/Blueqat">Blueqat/Blueqat</a></p><p>My LinkedIn account is here.</p><p><a href="https://www.linkedin.com/in/takumi-kato/">Takumi Kato - Quantum Computer Engineer (Quantum Algorithm, Simulator) - MDR | LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d313c067730" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>