<?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 Camille De Valk on Medium]]></title>
        <description><![CDATA[Stories by Camille De Valk on Medium]]></description>
        <link>https://medium.com/@camilledevalk?source=rss-65209be8c11a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1i7kkuaWoy9J-vTu9qFcRA.jpeg</url>
            <title>Stories by Camille De Valk on Medium</title>
            <link>https://medium.com/@camilledevalk?source=rss-65209be8c11a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 10:00:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@camilledevalk/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[Flipping the qubit — factoring in financial impact to quantum risk model]]></title>
            <link>https://medium.com/@camilledevalk/flipping-the-qubit-factoring-in-financial-impact-to-quantum-risk-model-b62380b7ffea?source=rss-65209be8c11a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b62380b7ffea</guid>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[ibm-quantum]]></category>
            <category><![CDATA[risk-modelling]]></category>
            <category><![CDATA[qiskit]]></category>
            <dc:creator><![CDATA[Camille De Valk]]></dc:creator>
            <pubDate>Tue, 05 Jul 2022 14:59:09 GMT</pubDate>
            <atom:updated>2022-07-05T14:59:09.682Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flipping the qubit — factoring in financial impact to quantum risk model</h3><p>In business, there are many different factors that can create risk, such as reputation, safety, financial, and climate as outlined in <a href="https://medium.com/@camilledevalk/getting-hands-on-with-modelling-climate-risk-using-quantum-and-qiskit-c6f9f7868618">the previous blog</a>. In this blog, we focus on the financial impact of our risk model.</p><p>In the previous blog, we explored how one can encode a risk model, (see the figure below), on a quantum computer. To reiterate, a qubit is used to represent a risk item, rotation gates are used to implement probabilities, and controlled rotation gates are used to implement transition probabilities. In this blog, we will add the financial element in the code and calculate the probability that a certain total impact is exceeded. This probability is one measure that can be estimated with amplitude estimation for quantum computing Monte Carlo, though, that is a topic for a later blog.</p><figure><img alt="climaterisk-small.png" src="https://cdn-images-1.medium.com/proxy/1*Abp9XiflIGU3LHW8oW8ifg.png" /></figure><p>Using Python and Qiskit, this risk graph can be encoded in the following quantum circuit. See <a href="https://qiskit.org/documentation/getting_started.html">qiskit’s webpage</a> to get started with qiskit.</p><pre>import numpy as np<br>import qiskit as q<br>import matplotlib.pyplot as plt<br>plt.style.use(&#39;ggplot&#39;)<br><br># Make separate quantum qubits (for the naming)<br>not_flooding = q.QuantumRegister(1, name=&#39;not flooding&#39;)<br>flooding = q.QuantumRegister(1, name=&#39;flooding&#39;)<br>fld_buildings = q.QuantumRegister(1, name=&#39;flooded buildings&#39;)<br>failed_h = q.QuantumRegister(1, name=&#39;failed harvest&#39;)<br><br># Helper converter function<br>p_to_theta = lambda x: 2*np.arcsin(np.sqrt(x))<br><br># Create the circuit<br>risk_model = q.QuantumCircuit(fld_buildings, failed_h, flooding, not_flooding, name=&#39;Risk Model&#39;)<br><br># Intrinsic not flooding<br>risk_model.rx(p_to_theta(0.8), not_flooding)<br><br># Add the CNOT with the control state as &#39;0&#39;<br>risk_model.cnot(not_flooding, flooding, ctrl_state=&#39;0&#39;)<br><br># The intrinsic probability for failed harvest occurs when there&#39;s no flooding<br>risk_model.crx(p_to_theta(0.1), flooding, failed_h, ctrl_state=&#39;0&#39;)<br><br># Calculate the P(failed harvest | flooding)<br>p = 1 - (1 - 0.1) * (1 - 0.5)<br>risk_model.crx(p_to_theta(p), flooding, failed_h, ctrl_state=&#39;1&#39;)<br><br># The same for flooded buildings<br>risk_model.crx(p_to_theta(0.05), flooding, fld_buildings, ctrl_state=&#39;0&#39;)<br>p = 1 - (1 - 0.05) * (1 - 0.4)<br>risk_model.crx(p_to_theta(p), flooding, fld_buildings, ctrl_state=&#39;1&#39;)</pre><pre># Draw the circuit with matplotlib<br>risk_model.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*z0rFbvHglpi00iX1gt5iQw.png" /></figure><p>But this circuit does not take into account the financial impacts of the risks, as shown in the figure. For example, there is no way to tell that risk item 4 has a financial impact of €8M and risk item 1 only has an impact of €1M. To be able to account for this, the numbers need to be added in the quantum computer.</p><h4>Adding numbers on a quantum computer</h4><p>In order to add the numbers, weighted by their relative impacts, we have to consider how one can count on a computer in general. On a classical computer, counting is done in binary. For a refresher on binary, read up in <a href="https://en.wikipedia.org/wiki/Binary_number">this Wikipedia article</a>.</p><p>On our quantum computer, we also count in binary. First, we create a register sum, where we count up all the impacts.</p><pre># Create the quantum registers<br>risk_register = q.QuantumRegister(risk_model.num_qubits, name=&#39;risk_model&#39;)<br>sum_register = q.QuantumRegister(4, name=&#39;sum&#39;)<br><br># Create the circuit<br>circuit = q.QuantumCircuit(risk_register, sum_register, name=&#39;Loss Distribution&#39;)<br><br># Append the previously defined risk model<br>circuit.append(risk_model, risk_register)<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*fTMzb9T7aU6oYqQ7b0uMyg.png" /></figure><p>Let’s say we start with our smallest-impact risk item, item 2. We want to make our adder as general as possible, so we don’t assume the register is in the all |0⟩ state. When we want to add the number <em>1</em> to our sum register, first we have to check if the sum register is <em>0111</em>. If it is, we need to flip the first bit and flip all the previous bits. Then we check for <em>0011</em>, then for <em>0001</em>, and if all these cases don’t apply, we flip the first bit. Note that the controlled-not gates also flip the qubits back to |0⟩ if necessary.</p><pre># The case where the sum register is in the state |0111&gt;<br>circuit.mcx(control_qubits=[risk_register[2], *sum_register[[0,1,2]]], target_qubit=sum_register[3])<br><br># The case where the sum register is in the state |0011&gt;<br>circuit.mcx(control_qubits=[risk_register[2], *sum_register[[0,1]]], target_qubit=sum_register[2])<br><br># The case where the sum register is in the state |0001&gt;<br>circuit.mcx(control_qubits=[risk_register[2], *sum_register[[0]]], target_qubit=sum_register[1])<br><br># The case where the sum register is in the state |0000&gt;<br>circuit.cx(risk_register[2], sum_register[0])<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*GFs-HWUiMrGs2Q5j78qjNA.png" /></figure><p>Now we need to add the numbers 4 and 8. These are easier to do, because only the first few <em>sum</em>-qubits need to be considered. A similar way of adding these numbers is used. First check if the qubits before the value are |1⟩, and if they are, flip the qubit.</p><pre># First add 4 for risk_model_1<br>circuit.barrier()<br><br># The case where the sum register is in the state |01xx&gt;<br>circuit.mcx(control_qubits=[risk_register[1], *sum_register[[2]]], target_qubit=sum_register[3])<br><br># The case where the sum register is in the state |00xx&gt;<br>circuit.cx(risk_register[1], sum_register[2])<br><br># Then add 8 for risk_model_0<br>circuit.barrier()<br># The case where the sum register is in the state |0xxx&gt;<br>circuit.cx(risk_register[0], sum_register[3])<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*Zr19Y-pAEntcXyZt82FLbQ.png" /></figure><p>And now the sum register contains a <em>weighted superposition of all the possible combinations of losses</em>, coming from the risk model in the figure. We can test this with the following code, which measures the sum register and runs the code with Qiskit&#39;s Aer simulator. Note that in quantum computing, when you want to estimate a distribution from a weighted superposition, you have to sample, just as in classical computing. In the next blog, we go into amplitude estimation, which offers a way to estimate certain measures with a quadratic speed-up.</p><pre># Convert circuit to composed circuit<br>loss_distribution = circuit.copy()<br><br># Create circuit for measurement<br>creg = q.ClassicalRegister(4, name=&#39;c&#39;)<br>circuit_final = q.QuantumCircuit(risk_register, sum_register, creg)<br>circuit_final.append(loss_distribution, [*risk_register, *sum_register])<br>circuit_final.measure(sum_register, creg)<br><br>backend = q.Aer.get_backend(&quot;aer_simulator&quot;)  # Get the quantum backend<br><br># Transpilation<br>circuit_transpiled = q.transpile(circuit_final, backend=backend)<br><br># Choose shots to run the quantum circuit<br>shots = 2048<br><br>job = backend.run(circuit_transpiled, shots=shots)  # Run the job<br>result = job.result()  # Get the result<br><br>counts = result.get_counts()  # Get the counts</pre><pre># Visualise the results<br>fig, ax = plt.subplots(dpi=150)<br><br># Get losses and probabilities<br>losses = np.array(list(counts.int_outcomes().keys()))<br>probabilites = np.array(list(counts.values()))/shots<br><br># Plot the bar chart<br>ax.bar(losses, probabilites)<br><br># Set the axes<br>ax.set_xlabel(&#39;Total impact (M€)&#39;)<br>ax.set_ylabel(&#39;Probability&#39;)<br><br>ax.set_xlim(-0.5,15.5)<br>ax.set_ylim(0,1)<br><br># Set title<br>ax.set_title(&#39;Loss distribution of risk model&#39;)<br><br>plt.show()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*rWK5S7hs3YzyK0yMNm7kOw.png" /></figure><p>Let’s review, what have we done. We loaded a risk model and added all the impacts. Now we have a superposition of all possible total impacts in the sum register. By measuring this (the code above), we destroy the superposition, which is not useful for us. We can use the superposition in the sum register to calculate all sorts of measures, like Value-at-Risk, Expected Shortfall, or Expected Loss. Here we look at the probability of exceeding maximum acceptable impact.</p><h4>Probability of exceeding impact</h4><p>Let’s say you have a maximum financial impact that a business can take. In our example this is €12M. In binary, 12 is 1100, which means that qubit 2 and 3 from the sum register will be in state |1⟩ when this impact is exceeded.</p><p>A new marking qubit will be controlled bit-flipped if qubits 2 and 3 in the sum register are in the |1⟩ state.</p><pre># Create marking qubit<br>mark = q.QuantumRegister(1, name=&#39;mark&#39;)<br><br># Create Quantum Circuit<br>circuit = q.QuantumCircuit(risk_register, sum_register, mark)<br><br># Add loss distribution<br>circuit.append(loss_distribution, [*risk_register, *sum_register])<br><br># Flip marking qubit<br>circuit.mcx(control_qubits=[*sum_register[[2,3]]], target_qubit=mark)<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*NNyC6mQTMsThOMgSelwbZg.png" /></figure><p>Now the marking qubit will be in the following quantum state</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dXzipFColEzWknwCDU5uUw.png" /></figure><p>The next challenge is to get out the amplitude <em>P</em>(total impact ≥ 12) from the quantum state |ψ⟩(mark). For now, we will do this by measuring the qubit a number of times in order to estimate <em>P</em>(total impact ≥ 12).</p><pre># Convert circuit to composed circuit<br>prob_exceeding_impact = circuit.copy()<br><br># Create circuit for measurement<br>creg = q.ClassicalRegister(1, name=&#39;c&#39;)<br>circuit_final = q.QuantumCircuit(risk_register, sum_register, mark, creg)<br>circuit_final.append(prob_exceeding_impact, [*risk_register, *sum_register, mark])<br>circuit_final.measure(mark, creg)<br><br>backend = q.Aer.get_backend(&quot;aer_simulator&quot;)  # Get the quantum backend<br><br># Transpilation<br>circuit_transpiled = q.transpile(circuit_final, backend=backend)<br><br># Choose shots to run the quantum circuit<br>shots = 4096<br><br>job = backend.run(circuit_transpiled, shots=shots)  # Run the job<br>result = job.result()  # Get the result<br><br>counts = result.get_counts()  # Get the counts<br><br>q.visualization.plot_histogram(counts)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*fqEl5a8_Mw0NizEdXJMajA.png" /></figure><p>From this we can find the result:</p><p><em>P</em>(total impact ≥ 12) ≈ 5%.</p><h4>Conclusion</h4><p>This result is something that we could’ve arrived at classically with great precision. Why then quantum? In the next blog, we will dive into quantum amplitude estimation, with which we can arrive at the above result <em>quadratically</em> faster than with a classical computer.</p><h4>References</h4><p>[1] Braun, M. C., Decker, T., Hegemann, N., Kerstan, S. F., &amp; Schäfer, C. (2021). <em>A Quantum Algorithm for the Sensitivity Analysis of Business Risks</em>. <a href="http://arxiv.org/abs/2103.05475">http://arxiv.org/abs/2103.05475</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b62380b7ffea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting hands on with modelling climate risk using quantum and qiskit]]></title>
            <link>https://medium.com/@camilledevalk/getting-hands-on-with-modelling-climate-risk-using-quantum-and-qiskit-c6f9f7868618?source=rss-65209be8c11a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6f9f7868618</guid>
            <category><![CDATA[risk-modelling]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[ibm-quantum]]></category>
            <category><![CDATA[qiskit]]></category>
            <dc:creator><![CDATA[Camille De Valk]]></dc:creator>
            <pubDate>Fri, 10 Jun 2022 13:27:55 GMT</pubDate>
            <atom:updated>2022-06-10T13:27:55.065Z</atom:updated>
            <content:encoded><![CDATA[<p>Climate change is impacting all of us. More and more are we experiencing its effects and it is expected that the climate will only keep changing. This means that the climate is no longer a stable factor, making climate risk assessment increasingly harder.</p><p>Climate risk can be thought of as risk caused by extreme weather events, for example floodings, or draughts, see the figure. These events occur with a probability that is usually unknown and must be estimated. The estimation of these probabilities remains a story for another time (hint: climate models are complex), for now we act as if these probabilities are given. The exercise becomes even more complex when taking into account the <em>transition probabilities</em>, that arise when weather events follow up on each other. For example, buildings might flood with a certain probability, but when there is a large flooding in the area, the probability that the building will flood increases. Besides the probabilities, the risk events also have a financial impact associated with them. Classically, one would create the risk model and every extra risk item would exponentially increase the size of the problem.</p><figure><img alt="climaterisk-small.png" src="https://cdn-images-1.medium.com/proxy/1*Abp9XiflIGU3LHW8oW8ifg.png" /></figure><p>When there are probabilities and exponential computational costs present, one might start to explore the world of quantum computing. Quantum computers are probabilistic by nature and offer a speed-up for some problems. In this blog, we will look at climate risk modelling on a quantum computer and get hands-on with implementing it on IBM’s Aer quantum simulator using Qiskit. The implementations in this blog will not give you a theoretical speed-up over classical methods. But later in the blog series, we will look at quantum Monte Carlo using quantum amplitude estimation, which has a theoretical quadratic speed-up.</p><h4>Probabilities on a quantum computer</h4><p>Quantum computers are fundamentally different than classical computers. A quantum computer uses quantum mechanics in three ways: superposition, entanglement and interference. Here, we focus mostly on superposition. A quantum bit, or qubit, can be expressed as a |0⟩ or a |1⟩, like the binary representations of a classical bit 0 and 1. Unlike the classical bit, a qubit can also be a linear combination of the two:</p><p>|ψ⟩=α|0⟩+β|1⟩,</p><p>where |ψ⟩ is the <em>quantum state</em> of the qubit and α,β∈C are complex amplitudes, such that |𝛼|^2+|𝛽|^2=1. The last condition is called <em>normalisation</em>. When the quantum state |ψ⟩ has both a factor |0⟩ and |1⟩, one might think of this as the qubit being in two states at the same time. This is called superposition.</p><p>Unfortunately for us, the total quantum state |ψ⟩ cannot be measured directly. When measuring, only the |0⟩ or the |1⟩ state can be measured, and fortunately, we know the probabilities of measuring |0⟩ or the |1⟩. These correspond directly to the amplitudes, by</p><p>𝑃(measuring |0⟩)=|𝛼|^2, 𝑃(measuring |1⟩)=|𝛽|^2.</p><p>This means we can encode a probability 𝑃 in a quantum computer, by carefully choosing 𝛼 and 𝛽:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*mQs2Q3kxbvRwLMb7syiNzA.png" /></figure><p>Now the probability of measuring the |1⟩ state is exaclty 𝑃.</p><h4>Rotation gates</h4><p>How can we encode the probability 𝑃 in our qubit? How can we specify a certain 𝛽? To do this, one can use an <em>X</em>-rotation gate, i.e. a <em>RX</em>(𝜃)-gate. The <em>RX</em>(𝜃)-gate will bring a qubit that’s starting in the|0⟩ to the state</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*K5GqOYQQctnF1jMjsbIEqQ.png" /></figure><p>which we can alter in order to get the desired result. By setting</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*p6PSFWgW-0X4GrhynsBW5w.png" /></figure><p>the probability of measuring the qubit in the |1⟩ state is exactly <em>P</em>, which you can confirm for yourself<em>.</em></p><p>This fact can be demonstrated on a quantum computer using qiskit and python, see <a href="https://qiskit.org/documentation/getting_started.html">qiskit’s webpage</a> to get started with qiskit.</p><pre>import qiskit as q<br>import numpy as np<br>import matplotlib.pyplot as plt<br>plt.style.use(&#39;ggplot&#39;)<br><br># Create the quantum circuit with 1 qubit<br>circuit = q.QuantumCircuit(1)<br><br>p = 0.8  # probability of 0.8<br>theta = 2 * np.arcsin(np.sqrt(p))  # from p to theta<br><br># Add the x-rotation gate to qubit 0<br>circuit.rx(theta, 0)<br><br># Measure the qubits to get the output<br>circuit.measure_all()<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*XhzRHdQ8H2Mqh7UupA2X4w.png" /></figure><p>Now we have a quantum circuit circuit and we know what it looks like. We first apply a rotation, and then we measure. The theoretical expectation is that we measure |1⟩ in p=0.8 = 80% of the runs. Using qiskit&#39;s Aer quantum simulator, we can run the circuit.</p><pre># Get the quantum backend<br>backend = q.Aer.get_backend(&quot;aer_simulator&quot;)<br>job = backend.run(circuit)  # Run the job<br><br>counts = job.result().get_counts()  # Get the results (counts)<br>q.visualization.plot_histogram(counts)  #Visualise the counts</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*2qo0uwilIF3dIAAKrU66gQ.png" /></figure><p>We can see that the expectation comes true. We measure the |1⟩ state in approximately 80% of the times. Note that this is not exact, this is because by measuring, we’re approximating the value of 𝛽 using a random (quantum) process. The approximation of 𝛽 will be better when repeating the experiment more often.</p><h4>Transition probabilities</h4><h4>XOR</h4><p>One important feature of quantum computers is the ability to construct multi-qubit gates. These allow us to create links between qubit states, this is called entanglement. In the figure of the climate risk model above, for example, we see that risk event 1 and event 2 are <em>always the opposite</em> (XOR or exclusive OR). On a quantum computer, the XOR can be achieved by using a controlled-NOT gate. The controlled-NOT gate flips the state of a target qubit (in state|ψ⟩ =α|0⟩+β|1⟩ if and only if a control qubit is in the |1⟩ state, i.e.,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/249/1*0xGuHJUnsUiX4Uey7oHhvQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*Ro_hyKWeSB5aA_ZJ0cao7A.png" /></figure><p>Note that interesting quantum states arise when the control qubit is in a superposition. Now we will implement the XOR to implement the first two risk items of the model.</p><pre># A new quantum circuit with 2 qubits<br>circuit = q.QuantumCircuit(2)<br><br># Add the x-rotation gate to qubit 0<br>circuit.rx(theta, 0)<br><br># Add the CNOT to implement the XOR<br>circuit.cnot(0,1)<br><br># Measure the qubits to get the output<br>circuit.measure_all()<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*0GkuGwgo_5H9FhL4D50Mlg.png" /></figure><p>And again, we can run this circuit and measure its outcomes.</p><pre>job = backend.run(circuit)  # Run the job<br><br>counts = job.result().get_counts()  # Get the results (counts)<br>q.visualization.plot_histogram(counts)  # Visualise the counts</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*JNF42FtSwiDv-l6S3lalCg.png" /></figure><p>That doesn’t look good! Now the qubits are always the same. Because qubit 1 started in the |0⟩ state, when it was flipped (i.e. when qubit 0 was in the |1⟩ state), it also became |1⟩. We need exactly the opposite behaviour, which can be achieved multiple ways. For consistency later on, we will alter the method in the following way: we change the CNOT-gate a bit, such that it flips the target qubit when the control is in the |0⟩ state.</p><pre># The same circuit as above<br>circuit = q.QuantumCircuit(2)<br>circuit.rx(theta, 0)<br><br># Add the CNOT with the control state as &#39;0&#39;<br>circuit.cnot(0,1, ctrl_state=&#39;0&#39;)<br><br># Measure the qubits to get the output<br>circuit.measure_all()<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*cko814St_Ubpclx8W4Yfhw.png" /></figure><p>Now we measure:</p><pre># The not-controlled-not gate is not available on all backends, so the circuit must be transpiled first.<br># We will keep this step in general as we&#39;re using more gates that are unavailable on all backends.<br>circuit_transpiled = q.transpile(circuit, backend=backend)<br><br>job = backend.run(circuit_transpiled)  # Run the job<br><br>counts = job.result().get_counts()  # Get the results (counts)<br>q.visualization.plot_histogram(counts)  # Visualise the counts</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*posyz-KLTuC9KlVS3iZHSQ.png" /></figure><p>That looks better! The qubits are always the opposite and the probabilities are taken into account.</p><h4>Controlled rotation</h4><p>We’ve got the first part covered: we can implement probabilities and XORs. In the climate risk model, the important feature is that the occurrence of one event, for example flooding, impacts the probabilities that other events occur. This feature can be implemented in a circuit by a controlled rotation. A controlled rotation applies a rotation to a target qubit when the control qubit is in a state. In the example, when the qubit for ‘flooding’ is in the |1⟩ state, there is a 40% probability that buildings will flood. This looks like the following:</p><pre># Make separate quantum qubits for the naming<br>flooding = q.QuantumRegister(1, name=&#39;flooding&#39;)<br>fld_buildings = q.QuantumRegister(1, name=&#39;flooded buildings&#39;)<br><br># Create the circuit<br>circuit = q.QuantumCircuit(flooding, fld_buildings)<br><br># Bring flooding in the |1&gt; state<br>circuit.x(flooding)<br><br># Get transition probability<br>transition_p = 0.4<br>theta_trans = 2 * np.arcsin(np.sqrt(0.4))<br><br># Control rotate the &#39;flooded buidlings&#39; qubit<br>circuit.crx(theta_trans, flooding, fld_buildings)<br><br># Measure the qubits to get the output<br>circuit.measure_all()<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*FhOTzbMB-bz7PNCQ84sWqw.png" /></figure><pre># Transpilation<br>circuit_transpiled = q.transpile(circuit, backend=backend)<br><br>job = backend.run(circuit_transpiled)  # Run the job<br><br>counts = job.result().get_counts()  # Get the results (counts)<br>q.visualization.plot_histogram(counts)  #Visualise the counts</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*IUJyH6zkhKnyg1EiRoyNKQ.png" /></figure><p>This is what we want: approximately 40% of the time, they both happen.</p><p>The intrinsic probabilities of the risk items make the model somewhat more complex. When ‘flooding’ is in the |1⟩ state, ‘flooded buidlings’ can happen because of a transition, or because of the intrinsic probability (or both). This means the <em>P</em> used to control 𝜃 must be altered according to the rules of probability. Also, there must be a controlled rotation for when the control qubit is in the |1⟩ state, but also for the |0⟩ state. The probabilities that should be encoded in these controlled rotations can be found as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7DQ6tQECz97_-qakIP8Rvg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79pvLj_pe3VuKEReFdYEdA.png" /></figure><p>and we have</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*0Qw7RTM3GisEj7JyqvJz0A.png" /></figure><p>We can now combine all of this to implement the entire risk model in the quantum circuit. [1]</p><pre># Make separate quantum qubits (for the naming)<br>not_flooding = q.QuantumRegister(1, name=&#39;not flooding&#39;)<br>flooding = q.QuantumRegister(1, name=&#39;flooding&#39;)<br>fld_buildings = q.QuantumRegister(1, name=&#39;flooded buildings&#39;)<br>failed_h = q.QuantumRegister(1, name=&#39;failed harvest&#39;)<br><br># Helper converter function<br>p_to_theta = lambda x: 2*np.arcsin(np.sqrt(x))<br><br># Create the circuit<br>circuit = q.QuantumCircuit(fld_buildings, failed_h, flooding, not_flooding)<br><br># Intrinsic not flooding<br>circuit.rx(p_to_theta(0.8), not_flooding)<br><br># Add the CNOT with the control state as &#39;0&#39;<br>circuit.cnot(not_flooding, flooding, ctrl_state=&#39;0&#39;)<br><br># The intrinsic probability for failed harvest occurs when there&#39;s no flooding<br>circuit.crx(p_to_theta(0.1), flooding, failed_h, ctrl_state=&#39;0&#39;)<br><br># Calculate the P(failed harvest | flooding)<br>p = 1 - (1 - 0.1) * (1 - 0.5)<br>circuit.crx(p_to_theta(p), flooding, failed_h, ctrl_state=&#39;1&#39;)<br><br># The same for flooded buildings<br>circuit.crx(p_to_theta(0.05), flooding, fld_buildings, ctrl_state=&#39;0&#39;)<br>p = 1 - (1 - 0.05) * (1 - 0.4)<br>circuit.crx(p_to_theta(p), flooding, fld_buildings, ctrl_state=&#39;1&#39;)<br><br># Measure the qubits to get the output<br>circuit.measure_all()<br><br># Draw the circuit with matplotlib<br>circuit.draw(output=&#39;mpl&#39;)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*2ojRKweZHFh-oYVm_s8skg.png" /></figure><p>And now this is our risk model. The 4 qubits are now in an entangled, superposition state corresponding to the risk model. Using the quantum circuit, the probabilities of risk items occurring can be estimated.</p><pre># Transpilation<br>circuit_transpiled = q.transpile(circuit, backend=backend)<br><br>job = backend.run(circuit_transpiled)  # Run the job<br><br>counts = job.result().get_counts()  # Get the results (counts)<br>q.visualization.plot_histogram(counts)  # Visualise the counts</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*U80AHrwZC8iKZyXyn1NoUg.png" /></figure><h4>Conclusion</h4><p>To conclude, we started with a risk model where risk items had intrinsic and transition probabilities. Using rotation gates and controlled rotation gates, we managed to encode the risk model in a quantum circuit. The quantum circuit has as many qubits as there are risk items in the model and it has two controlled gates for every <em>link</em> in the model.</p><p>From this, we can go down different routes. For example, we can use an adder circuit to add all the impacts of the risk events. This allows for the assessment of the financial risk coming from the risk model. We can also add factors like climate change, that will have a transition probability to, for example, flooding. These additions to the model remain the subject for future blogs.</p><h4>References</h4><p>[1] Braun, M. C., Decker, T., Hegemann, N., Kerstan, S. F., &amp; Schäfer, C. (2021). <em>A Quantum Algorithm for the Sensitivity Analysis of Business Risks</em>. <a href="http://arxiv.org/abs/2103.05475">http://arxiv.org/abs/2103.05475</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6f9f7868618" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>