<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aman Raza</title>
    <description>The latest articles on DEV Community by Aman Raza (@amanraza).</description>
    <link>https://dev.to/amanraza</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F434621%2Fb59b9f40-d084-432f-a587-d2fb335453a6.jpg</url>
      <title>DEV Community: Aman Raza</title>
      <link>https://dev.to/amanraza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amanraza"/>
    <language>en</language>
    <item>
      <title>Building a Quantum Coin Toss CLI with Python, Qiskit, and Matplotlib</title>
      <dc:creator>Aman Raza</dc:creator>
      <pubDate>Mon, 15 Jun 2026 07:34:57 +0000</pubDate>
      <link>https://dev.to/amanraza/building-a-quantum-coin-toss-cli-with-python-qiskit-and-matplotlib-1cii</link>
      <guid>https://dev.to/amanraza/building-a-quantum-coin-toss-cli-with-python-qiskit-and-matplotlib-1cii</guid>
      <description>&lt;h2&gt;
  
  
  Building a Quantum Coin Toss CLI with Python, Qiskit, and Matplotlib
&lt;/h2&gt;

&lt;p&gt;When people first hear about quantum computing, it can sound abstract and&lt;br&gt;
intimidating. Words like superposition, measurement, and qubits feel far away&lt;br&gt;
from the kind of code we usually write.&lt;/p&gt;

&lt;p&gt;So I built a small Python project to make one quantum idea more concrete: a&lt;br&gt;
quantum coin toss.&lt;/p&gt;

&lt;p&gt;The project uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python for the command-line application&lt;/li&gt;
&lt;li&gt;Qiskit to build and simulate a quantum circuit&lt;/li&gt;
&lt;li&gt;Matplotlib to visualize the measurement results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is simple: start with one qubit, put it into superposition, measure it,&lt;br&gt;
and treat the result as heads or tails.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Is Superposition?
&lt;/h2&gt;

&lt;p&gt;In classical computing, a bit is either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A qubit is different. Before measurement, it can exist in a combination of both&lt;br&gt;
states. This is called superposition.&lt;/p&gt;

&lt;p&gt;For this project, the qubit starts in the &lt;code&gt;|0&amp;gt;&lt;/code&gt; state. I use a Hadamard gate to&lt;br&gt;
turn it into an equal superposition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(|0&amp;gt; + |1&amp;gt;) / sqrt(2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That means when we measure the qubit, there is about a 50% chance of getting&lt;br&gt;
&lt;code&gt;0&lt;/code&gt; and about a 50% chance of getting &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; means heads&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; means tails&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The Quantum Circuit
&lt;/h2&gt;

&lt;p&gt;The core circuit is very small:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qiskit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;QuantumCircuit&lt;/span&gt;

&lt;span class="n"&gt;circuit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QuantumCircuit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;h&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;measure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what each line does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;circuit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QuantumCircuit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a circuit with one qubit and one classical bit.&lt;/p&gt;

&lt;p&gt;The qubit is the quantum part of the system. The classical bit stores the final&lt;br&gt;
measurement result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;h&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This applies a Hadamard gate to qubit &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The Hadamard gate is the key operation in this project. It changes the qubit&lt;br&gt;
from a definite &lt;code&gt;|0&amp;gt;&lt;/code&gt; state into an equal superposition of &lt;code&gt;|0&amp;gt;&lt;/code&gt; and &lt;code&gt;|1&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;measure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This measures qubit &lt;code&gt;0&lt;/code&gt; and stores the result in classical bit &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Measurement is where the quantum state collapses into one classical result:&lt;br&gt;
either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Turning It Into a CLI
&lt;/h2&gt;

&lt;p&gt;I wanted this to be more than a notebook experiment, so I turned it into a&lt;br&gt;
command-line application.&lt;/p&gt;

&lt;p&gt;The CLI supports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;choosing the number of shots&lt;/li&gt;
&lt;li&gt;setting a simulator seed&lt;/li&gt;
&lt;li&gt;hiding the circuit diagram&lt;/li&gt;
&lt;li&gt;printing a short explanation&lt;/li&gt;
&lt;li&gt;showing or saving a Matplotlib chart&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--shots&lt;/span&gt; 5000 &lt;span class="nt"&gt;--plot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Qiskit, &lt;code&gt;shots&lt;/code&gt; means the number of times the circuit is executed. If we run&lt;br&gt;
only one shot, we get one coin toss. If we run 5000 shots, we get a distribution&lt;br&gt;
of many tosses.&lt;/p&gt;

&lt;p&gt;With enough shots, the results should usually be close to 50% heads and 50%&lt;br&gt;
tails.&lt;/p&gt;
&lt;h2&gt;
  
  
  Running the Simulation
&lt;/h2&gt;

&lt;p&gt;The simulation uses Qiskit's local Aer simulator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qiskit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;transpile&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;qiskit_aer&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AerSimulator&lt;/span&gt;

&lt;span class="n"&gt;simulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AerSimulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seed_simulator&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;compiled_circuit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;transpile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;circuit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;simulator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;simulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;compiled_circuit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shots&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;shots&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;result&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_counts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does a few important things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;AerSimulator&lt;/code&gt; gives us a local quantum simulator&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;transpile&lt;/code&gt; prepares the circuit for the simulator backend&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt; executes the circuit many times&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;get_counts&lt;/code&gt; returns how many times each result appeared&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The output might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Quantum coin toss results
Shots:       1024
Heads |0&amp;gt;:     515 ( 50.3%)
Tails |1&amp;gt;:     509 ( 49.7%)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact numbers can change from run to run because measurement is&lt;br&gt;
probabilistic.&lt;/p&gt;
&lt;h2&gt;
  
  
  Visualizing the Results
&lt;/h2&gt;

&lt;p&gt;I added Matplotlib so the result is easier to understand visually.&lt;/p&gt;

&lt;p&gt;The program can show a bar chart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--plot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or save the chart as an image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--save-plot&lt;/span&gt; results.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabdfqx7g1iote3ug6rfb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabdfqx7g1iote3ug6rfb.png" alt="Quantum Coin Toss Plot" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The visualization compares the number of &lt;code&gt;Heads |0&amp;gt;&lt;/code&gt; and &lt;code&gt;Tails |1&amp;gt;&lt;/code&gt;&lt;br&gt;
measurements. This makes the probabilistic behavior easier to explain,&lt;br&gt;
especially to someone seeing quantum computing for the first time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why This Project Matters
&lt;/h2&gt;

&lt;p&gt;This is a small project, but it connects several important ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;quantum superposition&lt;/li&gt;
&lt;li&gt;measurement and collapse&lt;/li&gt;
&lt;li&gt;probabilistic simulation&lt;/li&gt;
&lt;li&gt;Python CLI design&lt;/li&gt;
&lt;li&gt;data visualization&lt;/li&gt;
&lt;li&gt;packaging a script as a usable tool&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me, the most important lesson was this:&lt;/p&gt;

&lt;p&gt;The randomness does not come from Python's &lt;code&gt;random&lt;/code&gt; module. It comes from&lt;br&gt;
measuring a qubit after applying a quantum gate.&lt;/p&gt;

&lt;p&gt;That is what makes the project different from a normal classical coin toss.&lt;/p&gt;
&lt;h2&gt;
  
  
  Full CLI Examples
&lt;/h2&gt;

&lt;p&gt;Install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;qiskit qiskit-aer matplotlib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run with the default 1024 shots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run more tosses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--shots&lt;/span&gt; 10000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Print the explanation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--explain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save a result chart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python quantum_coin_toss.py &lt;span class="nt"&gt;--shots&lt;/span&gt; 5000 &lt;span class="nt"&gt;--save-plot&lt;/span&gt; results.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I Would Improve Next
&lt;/h2&gt;

&lt;p&gt;Some possible next steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compare a classical coin toss and a quantum coin toss&lt;/li&gt;
&lt;li&gt;run the circuit on real IBM Quantum hardware&lt;/li&gt;
&lt;li&gt;add unit tests for the CLI&lt;/li&gt;
&lt;li&gt;export results as CSV&lt;/li&gt;
&lt;li&gt;add a small Streamlit or web interface&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This project helped me understand quantum computing through code instead of&lt;br&gt;
only theory.&lt;/p&gt;

&lt;p&gt;By building a simple quantum coin toss, I learned how a qubit can be placed into&lt;br&gt;
superposition, how measurement produces a classical result, and how Qiskit can&lt;br&gt;
simulate that process locally.&lt;/p&gt;

&lt;p&gt;It is a small project, but it gave me a clearer mental model of one of the most&lt;br&gt;
important ideas in quantum computing.&lt;/p&gt;

&lt;p&gt;Github: &lt;a href="https://github.com/aman-raza/Quantum-Coin-Toss" rel="noopener noreferrer"&gt;Quantum-Coin-Toss&lt;/a&gt;&lt;/p&gt;

</description>
      <category>quantumcomputing</category>
      <category>python</category>
      <category>qiskit</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
