<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Microsoft on kenji.blog</title><link>http://kenji.blog/en/tags/microsoft/</link><description>Recent content in Microsoft on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>kenjinote</copyright><lastBuildDate>Sat, 12 Sep 2026 00:00:00 +0900</lastBuildDate><atom:link href="http://kenji.blog/en/tags/microsoft/index.xml" rel="self" type="application/rss+xml"/><item><title>I tried out Q#, a programming language for quantum computers</title><link>http://kenji.blog/en/p/qsharp-quantum-programming-language-intro/</link><pubDate>Sat, 12 Sep 2026 00:00:00 +0900</pubDate><guid>http://kenji.blog/en/p/qsharp-quantum-programming-language-intro/</guid><description>&lt;img src="http://kenji.blog/p/qsharp-quantum-programming-language-intro/img/eyecatch.jpg" alt="Featured image of post I tried out Q#, a programming language for quantum computers" />&lt;h2 id="1-introduction-the-dawn-of-quantum-computing-and-a-new-paradigm-of-programming">1. Introduction: The Dawn of Quantum Computing and a New Paradigm of Programming
&lt;/h2>&lt;p>In recent years, technological innovations in hardware and software in the field of quantum computing have been remarkable. While classical computers (such as the PCs, smartphones, and supercomputers we use daily) process information using combinations of deterministic bits that are either &amp;ldquo;0&amp;rdquo; or &amp;ldquo;1&amp;rdquo;, quantum computers directly utilize physical phenomena unique to quantum mechanics, such as &amp;ldquo;Superposition&amp;rdquo; and &amp;ldquo;Entanglement&amp;rdquo;, as the foundation of information processing. This has shown the potential to achieve computational speeds for specific classes of problems that would be unreachable for classical computers even over the lifespan of the universe, a concept known as &amp;ldquo;Quantum Supremacy&amp;rdquo; or &amp;ldquo;Quantum Advantage&amp;rdquo;. For example, dramatic reductions in computational complexity are expected in areas like factoring enormous numbers (Shor&amp;rsquo;s algorithm), high-speed database searches (Grover&amp;rsquo;s algorithm), quantum chemistry simulations (VQE algorithm), combinatorial optimization problems, and even specific processes in machine learning (Quantum Machine Learning).&lt;/p>
&lt;p>However, in order to draw out this astonishing potential of quantum computers as real-world applications, advances in physical hardware (such as superconducting qubits and ion traps) alone are not enough. A &amp;ldquo;quantum programming language&amp;rdquo; to accurately design quantum circuits and describe quantum algorithms flawlessly and efficiently, along with a robust development, execution, and debugging environment to support it, are indispensable. Classical programming languages (like C++, Python, Java, etc.) excel at abstracting the behavior of classical CPU architectures but were not designed to naturally describe the manipulation of non-deterministic quantum states with complex amplitudes.&lt;/p>
&lt;p>In this article, among the many quantum programming environments, we will focus on the &amp;ldquo;Quantum Development Kit (QDK)&amp;rdquo;, which is strongly promoted and developed as open-source by Microsoft, and its core, the dedicated programming language &amp;ldquo;Q#&amp;rdquo;.&lt;/p>
&lt;p>Q# was designed from scratch as a Domain Specific Language (DSL) specialized for describing quantum algorithms, absorbing the best parts of C#, F#, and Python. It features powerful capabilities that seamlessly integrate classical control flow (like if statements and for loops) with quantum operations (like applying gates and measurements). In this article, starting from the foundational mathematical models of quantum computing, we will thoroughly and deeply explain the linguistic features of Q#, comparisons of design philosophies with Python&amp;rsquo;s Qiskit, the actual coding to construct and measure a &amp;ldquo;Bell State (quantum entanglement)&amp;rdquo;, and even methods of integration with classical languages (Python and C#). By the time you finish reading this article, you will have understood the basics of quantum programming and be ready to start writing Q# code in your own environment.&lt;/p>
&lt;h2 id="2-mathematical-foundations-of-quantum-computing-state-superposition-and-entanglement">2. Mathematical Foundations of Quantum Computing: State, Superposition, and Entanglement
&lt;/h2>&lt;p>To deeply understand the syntax and features of Q# and write effective quantum programs, you first need to organize the mathematical foundational knowledge (especially linear algebra) behind quantum states and quantum gate operations. Here, we will overview the fundamental mathematical models essential for quantum programming.&lt;/p>
&lt;h3 id="21-qubits-and-superposition-states">2.1 Qubits and Superposition States
&lt;/h3>&lt;p>While a Classical Bit can only take one of the states $0$ or $1$, a quantum bit (Qubit) is represented as a Linear Combination, or &amp;ldquo;superposition&amp;rdquo;, of the states $|0\rangle$ and $|1\rangle$. Using bra-ket notation (Dirac notation) and complex coefficients $\alpha$ and $\beta$, this state is described as follows:&lt;/p>
$$ |\psi\rangle = \alpha|0\rangle + \beta|1\rangle $$
&lt;p>Here, $\alpha$ and $\beta$ are Complex Numbers called Probability Amplitudes, and when this qubit is measured, the probability of observing the state $|0\rangle$ is $|\alpha|^2$, and the probability of observing the state $|1\rangle$ is $|\beta|^2$. As a physical constraint, the sum of the probabilities of observing all possible states must always be $1$, so the following Normalization Condition must be satisfied:&lt;/p>
$$ |\alpha|^2 + |\beta|^2 = 1 $$
&lt;p>The state of a qubit is often visualized as a point on the surface of a unit sphere in 3D space called the &amp;ldquo;Bloch Sphere&amp;rdquo;. The north pole corresponds to $|0\rangle$ and the south pole to $|1\rangle$, while points on the equator represent states where $|0\rangle$ and $|1\rangle$ are superposed with equal probability (for example, the state with phase 0, $|+\rangle = \frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)$, or the state with phase $\pi/2$, $|i\rangle = \frac{1}{\sqrt{2}}(|0\rangle + i|1\rangle)$). Operations of quantum gates can be geometrically understood as rotation operations on this Bloch sphere.&lt;/p>
&lt;h3 id="22-multiple-qubits-tensor-products-and-quantum-entanglement">2.2 Multiple Qubits, Tensor Products, and Quantum Entanglement
&lt;/h3>&lt;p>The true power of quantum computing is unleashed when multiple qubits are combined. The state of a system consisting of multiple qubits is described by the &amp;ldquo;Tensor Product&amp;rdquo; of the state spaces of the individual qubits. For instance, the overall state of a two-qubit system looks like this:&lt;/p>
$$ |\psi\rangle = \alpha_{00}|00\rangle + \alpha_{01}|01\rangle + \alpha_{10}|10\rangle + \alpha_{11}|11\rangle $$
&lt;p>Here again, the normalization condition $\sum_{i,j} |\alpha_{ij}|^2 = 1$ holds. The important point is that to fully describe a system of n qubits, $2^n$ complex amplitudes are required. For example, even for a system of just 50 qubits, representing its state requires $2^{50} \approx 10^{15}$ complex numbers, which far exceeds the memory capacity of today&amp;rsquo;s fastest supercomputers. This is one of the reasons why quantum computers have an exponential advantage over classical computers.&lt;/p>
&lt;p>&amp;ldquo;Quantum Entanglement&amp;rdquo; refers to a state in such multi-qubit systems that cannot be simply decomposed (factored) as a tensor product of the states of individual qubits. One of the most famous and important entangled states is the following &amp;ldquo;Bell State&amp;rdquo;:&lt;/p>
$$ |\Phi^+\rangle = \frac{1}{\sqrt{2}}(|00\rangle + |11\rangle) $$
&lt;p>In this state, the moment one of the qubits is measured and yields $0$ (or $1$), the state of the other qubit is instantaneously determined to be $0$ (or $1$), regardless of distance. This non-local correlation, which Einstein called &amp;ldquo;spooky action at a distance&amp;rdquo;, is a fundamental resource in quantum teleportation, superdense coding, quantum cryptography, and the efficient execution of many quantum algorithms. In later sections, we will actually create this Bell state using Q#.&lt;/p>
&lt;h3 id="23-quantum-gate-operations-and-unitary-matrices">2.3 Quantum Gate Operations and Unitary Matrices
&lt;/h3>&lt;p>Operations that change quantum states (equivalent to AND, OR, and NOT gates in classical logic circuits) are called quantum gates. Mathematically, a quantum gate is represented as a matrix of complex numbers and acts as a matrix multiplication on the vector of the quantum state. According to the axioms of quantum mechanics, these matrices must always be Unitary Matrices (matrices satisfying $U^\dagger U = I$, where $U^\dagger$ is the adjoint matrix and $I$ is the identity matrix). Consequently, all quantum operations other than measurements are Reversible.&lt;/p>
&lt;p>Typical single-qubit gates:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Pauli-X Gate (NOT Gate)&lt;/strong>: Flips $|0\rangle$ to $|1\rangle$ and $|1\rangle$ to $|0\rangle$.
$$ X = \begin{pmatrix} 0 &amp; 1 \\ 1 &amp; 0 \end{pmatrix} $$&lt;/li>
&lt;li>&lt;strong>Pauli-Z Gate (Phase Shift Gate)&lt;/strong>: Leaves $|0\rangle$ unchanged and flips the sign of $|1\rangle$ (adds $\pi$ to the relative phase).
$$ Z = \begin{pmatrix} 1 &amp; 0 \\ 0 &amp; -1 \end{pmatrix} $$&lt;/li>
&lt;li>&lt;strong>Hadamard Gate (H Gate)&lt;/strong>: Converts deterministic states into superposition states.
$$ H = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 &amp; 1 \\ 1 &amp; -1 \end{pmatrix} $$&lt;/li>
&lt;/ul>
&lt;p>Typical two-qubit gates:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>CNOT Gate (Controlled-NOT Gate)&lt;/strong>: Applies an X gate (NOT operation) to the Target Qubit only when the Control Qubit is $|1\rangle$.
$$ CNOT = \begin{pmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0 &amp; 1 &amp; 0 &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp; 1 \\ 0 &amp; 0 &amp; 1 &amp; 0 \end{pmatrix} $$&lt;/li>
&lt;/ul>
&lt;p>Quantum algorithms can be said to be the design of processes that combine these basic unitary matrices to realize a desired computation.&lt;/p>
&lt;h2 id="3-what-is-the-microsoft-quantum-development-kit-qdk">3. What is the Microsoft Quantum Development Kit (QDK)?
&lt;/h2>&lt;p>The Quantum Development Kit (QDK) provided by Microsoft is a comprehensive toolset designed to support the software development of quantum computing. It supports the entire development lifecycle, from designing, debugging, and optimizing quantum algorithms, to executing them on simulators or actual hardware.&lt;/p>
&lt;p>The QDK includes the following key elements:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Q# Compiler and Execution Environment&lt;/strong>: It heavily analyzes and optimizes code written in the Q# language, transforming it into formats (like QIR) executable on simulators or actual quantum hardware (via Azure Quantum). The Q# compiler performs static analysis specific to quantum computation, such as checking function purity and managing the lifecycle of qubits.&lt;/li>
&lt;li>&lt;strong>Quantum Simulator&lt;/strong>: It includes a Full State Simulator that simulates the evolution of quantum states on the developer&amp;rsquo;s local machine. This allows you to rapidly test and debug small-scale algorithms up to a few dozen qubits locally. Additionally, a Resource Estimator is provided to estimate resource requirements for large-scale circuits (thousands to millions of qubits).&lt;/li>
&lt;li>&lt;strong>Rich Libraries&lt;/strong>: The Q# Standard Library offers various advanced building blocks, ranging from basic quantum gates (H, X, Y, Z, CNOT, etc.) to complex arithmetic operations (like quantum adders), Amplitude Amplification, and Quantum Phase Estimation algorithms. This saves developers from reinventing the wheel.&lt;/li>
&lt;li>&lt;strong>Integrated Development Environment (IDE) Integration&lt;/strong>: Extensions for Visual Studio and Visual Studio Code are provided, allowing you to use indispensable features for modern software development, such as syntax highlighting, code completion (IntelliSense), powerful debugging tools, and integration with testing frameworks.&lt;/li>
&lt;/ol>
&lt;p>Below is a Mermaid diagram showing the workflow from writing a Q# program to its execution on hardware.&lt;/p>
&lt;div class="mermaid">graph TD
A["Q# Source Code (.qs)"] --> B["Q# Compiler"]
B --> C["Quantum Intermediate Representation (QIR)"]
C --> D["Local Quantum Simulator"]
C --> E["Azure Quantum Service"]
E --> F["IonQ Hardware"]
E --> G["Quantinuum Hardware"]
E --> H["Rigetti Hardware"]
D --> I["Result / Output Data"]
F --> I
G --> I
H --> I
style A fill:#e1f5fe,stroke:#03a9f4,stroke-width:2px;
style B fill:#fff3e0,stroke:#ff9800,stroke-width:2px;
style C fill:#f3e5f5,stroke:#9c27b0,stroke-width:2px;&lt;/div>
&lt;p>The extremely brilliant part of this architecture is that by going through an LLVM-based intermediate representation called QIR (Quantum Intermediate Representation), it completely abstracts away the differences in underlying hardware architectures (superconducting qubits, ion traps, topological qubits, photonic quantum, etc.). Developers can focus entirely on the pure logical design of their algorithms without worrying about the physical details of the hardware (the specific native gate sets and topology of each hardware). The compiler passes beyond the QIR layer automatically perform transpilation of gates optimized for the target hardware.&lt;/p>
&lt;h2 id="4-q-vs-pythonqiskit-why-do-we-need-a-new-language">4. Q# vs Python/Qiskit: Why Do We Need a New Language?
&lt;/h2>&lt;p>When learning quantum programming, due to its accessibility and the widespread use of Python, many people first interact with &amp;ldquo;Qiskit&amp;rdquo;, a Python-based framework developed by IBM. Qiskit is an incredibly powerful and widely used tool, but its fundamental design philosophy (paradigm) differs greatly from Microsoft&amp;rsquo;s Q#.&lt;/p>
&lt;h3 id="the-qiskit-approach-building-circuit-objects-with-python">The Qiskit Approach (Building Circuit Objects with Python)
&lt;/h3>&lt;p>Qiskit is essentially a &amp;ldquo;Python API library for building quantum circuits&amp;rdquo;. As developers execute a Python script, a sequence of quantum gates (a circuit object) is gradually assembled in memory. After adding all the gates, you finally Submit that massive circuit object to a backend (a local simulator or an actual machine on the cloud) for execution.
This metaprogramming-like approach has the major advantage of being extremely easy to integrate with the existing Python ecosystem (machine learning libraries like NumPy, SciPy, PyTorch, and visualization tools). However, when expressing complex control flows where classical and quantum elements are mixed—for instance, Dynamic Circuits like &amp;ldquo;measure a qubit, and only if the result is 1, apply a specific complex unitary operation to another group of qubits, and run a while loop&amp;rdquo;—you cannot use Python&amp;rsquo;s native if statements or for loops (because they get evaluated during &amp;ldquo;circuit construction time&amp;rdquo;). You end up having to use Qiskit&amp;rsquo;s own special control instructions, making the code highly complex and unintuitive.&lt;/p>
&lt;h3 id="the-q-approach-a-quantum-first-domain-specific-language">The Q# Approach (A Quantum-First Domain Specific Language)
&lt;/h3>&lt;p>On the other hand, Q# is a standalone compiled language designed from the ground up to treat quantum computation itself as a First-class citizen. In Q#, you can describe quantum bit allocation, gate operations, and measurements naturally and seamlessly within the same codebase, with the exact same feel as classical variable operations, if statements, and loops.
The Q# compiler statically analyzes the entire code, determines which parts should run on a classical computing device (host CPU or control electronics) and which parts on a quantum coprocessor (QPU), and performs heavy optimization. This allows for higher modularity, readability, maintainability, and Type Safety when implementing large-scale and complex quantum algorithms. Q# is a language not for &amp;ldquo;writing circuits&amp;rdquo;, but for &amp;ldquo;writing algorithms&amp;rdquo;.&lt;/p>
&lt;h2 id="5-deep-dive-into-qs-basic-syntax-and-distinctive-concepts">5. Deep Dive into Q#&amp;rsquo;s Basic Syntax and Distinctive Concepts
&lt;/h2>&lt;p>The syntax of Q# is a highly refined design, resembling a combination of the block structure using C#&amp;rsquo;s curly braces &lt;code>{}&lt;/code>, the elements of functional programming from F#, and powerful type inference. Here, we will detail the important keywords and concepts necessary for a deep understanding of Q#.&lt;/p>
&lt;h3 id="51-strict-distinction-between-operation-and-function">5.1 Strict Distinction Between &lt;code>operation&lt;/code> and &lt;code>function&lt;/code>
&lt;/h3>&lt;p>In Q#, &lt;code>operation&lt;/code> and &lt;code>function&lt;/code> are strictly separated to define blocks of processing (subroutines). This stems from the concept of &amp;ldquo;Purity&amp;rdquo; in functional programming.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>&lt;code>function&lt;/code>&lt;/strong>: A pure function that performs only Deterministic classical computation. Given the same input parameters, it will always return the exact same output result, no matter how many times it is executed. Inside a &lt;code>function&lt;/code>, quantum operations (operations with side effects) such as qubit allocation, gate application, or measurement will result in a compile error. It is used for mathematical function calculations and data transformations.&lt;/li>
&lt;li>&lt;strong>&lt;code>operation&lt;/code>&lt;/strong>: A non-deterministic routine that includes quantum computation. It involves qubit manipulation and measurements, meaning that even with the same input, the result may vary due to the probabilistic nature of quantum mechanics (like the collapse of the wave function upon measurement). The core parts of a quantum algorithm are all defined as &lt;code>operation&lt;/code>s.&lt;/li>
&lt;/ul>
&lt;h3 id="52-the-qubit-type-and-lifecycle-management-with-the-use-keyword">5.2 The &lt;code>Qubit&lt;/code> Type and Lifecycle Management with the &lt;code>use&lt;/code> Keyword
&lt;/h3>&lt;p>In Q#, qubits are treated as &amp;ldquo;Opaque&amp;rdquo; objects of the &lt;code>Qubit&lt;/code> type. Developers are intentionally prohibited from directly reading or rewriting the probability amplitudes of their internal state (e.g., the values of $\alpha$ or $\beta$) within the program (this aligns with the &amp;ldquo;measurement problem&amp;rdquo; in actual physical quantum systems). The only way to interact with a qubit is by invoking the provided quantum gate operations and measurement functions.&lt;/p>
&lt;p>To allocate a new qubit within a program, you use the &lt;code>use&lt;/code> keyword (it was called &lt;code>using&lt;/code> in older versions of Q#). The &lt;code>use&lt;/code> block clearly defines the scope and lifecycle of the qubit.
An important rule is that upon exiting a &lt;code>use&lt;/code> block, all qubits allocated within it must be completely returned to the $|0\rangle$ state (otherwise, a runtime exception will occur). This is a strong safety mechanism in Q# to ensure qubit reuse and prevent memory leaks.&lt;/p>
&lt;h3 id="53-measurement-m-and-the-convenient-mresetz">5.3 Measurement &lt;code>M&lt;/code> and the Convenient &lt;code>MResetZ&lt;/code>
&lt;/h3>&lt;p>Measurement operations, which convert quantum states into classical information (0 or 1), are performed with the basic operation called &lt;code>M&lt;/code>. The measurement result in the Z-basis (standard basis) is returned as an enumerated &lt;code>Result&lt;/code> type (with values &lt;code>Zero&lt;/code> or &lt;code>One&lt;/code>).
However, as mentioned earlier, qubits are required to be in the $|0\rangle$ state when released. If you simply perform a measurement &lt;code>M&lt;/code>, and the result happens to be &lt;code>One&lt;/code>, the qubit collapses to the $|1\rangle$ state. Therefore, in practical code, a convenient standard operation called &lt;code>MResetZ&lt;/code> is very frequently used to securely reset the state of the qubit back to $|0\rangle$ immediately after the measurement.&lt;/p>
&lt;h3 id="54-variable-immutability-and-mutable">5.4 Variable Immutability and &lt;code>mutable&lt;/code>
&lt;/h3>&lt;p>Strongly influenced by functional programming, all variables in Q# are Immutable by default. A variable once bound with the &lt;code>let&lt;/code> keyword cannot have its value changed thereafter. This helps reduce unintended side effects in parallel processing and quantum algorithms.
If you need to declare a variable whose value needs to be updated, such as a loop counter or accumulator, you explicitly use the &lt;code>mutable&lt;/code> keyword, and use the &lt;code>set&lt;/code> keyword to update the value.&lt;/p>
&lt;h2 id="6-practice-creating-and-measuring-a-bell-state-quantum-entanglement-in-q">6. Practice: Creating and Measuring a Bell State (Quantum Entanglement) in Q#
&lt;/h2>&lt;p>Now, let&amp;rsquo;s bring together all the knowledge we&amp;rsquo;ve learned so far and write a program in Q# to actually create and measure the &amp;ldquo;Bell State&amp;rdquo; explained in the math section. This is a very important step, essentially the &amp;ldquo;Hello World&amp;rdquo; of quantum programming.&lt;/p>
&lt;h3 id="designing-and-explaining-the-quantum-circuit">Designing and Explaining the Quantum Circuit
&lt;/h3>&lt;p>The standard quantum circuit procedure to create the Bell state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$ is as follows:&lt;/p>
&lt;ol>
&lt;li>Prepare two qubits $q_0$ and $q_1$ in the initial state $|00\rangle$.&lt;/li>
&lt;li>Apply a Hadamard gate (an $H$ gate) to $q_0$. This puts $q_0$ into an equal superposition of $|0\rangle$ and $|1\rangle$, $\frac{1}{\sqrt{2}}(|0\rangle + |1\rangle)$. At this point, the entire system&amp;rsquo;s state is $\frac{1}{\sqrt{2}}(|0\rangle + |1\rangle) \otimes |0\rangle = \frac{1}{\sqrt{2}}(|00\rangle + |10\rangle)$.&lt;/li>
&lt;li>Apply a CNOT (Controlled-NOT) gate using $q_0$ as the control bit and $q_1$ as the target bit. As a result, $q_1$ flips only when $q_0$ is $|1\rangle$. Consequently, the state $|00\rangle$ remains $|00\rangle$, and the state $|10\rangle$ becomes $|11\rangle$, making the final overall system state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$. This marks the completion of a fully correlated entangled state.&lt;/li>
&lt;/ol>
&lt;h3 id="implementation-code-in-q">Implementation Code in Q#
&lt;/h3>&lt;p>The following code is a practical implementation example that uses Q# to generate a Bell state, repeats the measurement experiment a specified number of times, and gathers its statistics (probability distribution).&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;span class="lnt">31
&lt;/span>&lt;span class="lnt">32
&lt;/span>&lt;span class="lnt">33
&lt;/span>&lt;span class="lnt">34
&lt;/span>&lt;span class="lnt">35
&lt;/span>&lt;span class="lnt">36
&lt;/span>&lt;span class="lnt">37
&lt;/span>&lt;span class="lnt">38
&lt;/span>&lt;span class="lnt">39
&lt;/span>&lt;span class="lnt">40
&lt;/span>&lt;span class="lnt">41
&lt;/span>&lt;span class="lnt">42
&lt;/span>&lt;span class="lnt">43
&lt;/span>&lt;span class="lnt">44
&lt;/span>&lt;span class="lnt">45
&lt;/span>&lt;span class="lnt">46
&lt;/span>&lt;span class="lnt">47
&lt;/span>&lt;span class="lnt">48
&lt;/span>&lt;span class="lnt">49
&lt;/span>&lt;span class="lnt">50
&lt;/span>&lt;span class="lnt">51
&lt;/span>&lt;span class="lnt">52
&lt;/span>&lt;span class="lnt">53
&lt;/span>&lt;span class="lnt">54
&lt;/span>&lt;span class="lnt">55
&lt;/span>&lt;span class="lnt">56
&lt;/span>&lt;span class="lnt">57
&lt;/span>&lt;span class="lnt">58
&lt;/span>&lt;span class="lnt">59
&lt;/span>&lt;span class="lnt">60
&lt;/span>&lt;span class="lnt">61
&lt;/span>&lt;span class="lnt">62
&lt;/span>&lt;span class="lnt">63
&lt;/span>&lt;span class="lnt">64
&lt;/span>&lt;span class="lnt">65
&lt;/span>&lt;span class="lnt">66
&lt;/span>&lt;span class="lnt">67
&lt;/span>&lt;span class="lnt">68
&lt;/span>&lt;span class="lnt">69
&lt;/span>&lt;span class="lnt">70
&lt;/span>&lt;span class="lnt">71
&lt;/span>&lt;span class="lnt">72
&lt;/span>&lt;span class="lnt">73
&lt;/span>&lt;span class="lnt">74
&lt;/span>&lt;span class="lnt">75
&lt;/span>&lt;span class="lnt">76
&lt;/span>&lt;span class="lnt">77
&lt;/span>&lt;span class="lnt">78
&lt;/span>&lt;span class="lnt">79
&lt;/span>&lt;span class="lnt">80
&lt;/span>&lt;span class="lnt">81
&lt;/span>&lt;span class="lnt">82
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-gdscript3" data-lang="gdscript3">&lt;span class="line">&lt;span class="cl">&lt;span class="n">namespace&lt;/span> &lt;span class="n">Quantum&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">BellState&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Import&lt;/span> &lt;span class="n">necessary&lt;/span> &lt;span class="n">namespaces&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">open&lt;/span> &lt;span class="n">Microsoft&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Quantum&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Intrinsic&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">open&lt;/span> &lt;span class="n">Microsoft&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Quantum&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Canon&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">open&lt;/span> &lt;span class="n">Microsoft&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Quantum&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Measurement&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">open&lt;/span> &lt;span class="n">Microsoft&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Quantum&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">Diagnostics&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1"># Summary&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="n">Generates&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">single&lt;/span> &lt;span class="n">Bell&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">measures&lt;/span> &lt;span class="n">two&lt;/span> &lt;span class="n">qubits&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">Z&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="n">basis&lt;/span>&lt;span class="o">.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1"># Output&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">Result&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Result&lt;/span>&lt;span class="p">):&lt;/span> &lt;span class="n">The&lt;/span> &lt;span class="n">measurement&lt;/span> &lt;span class="n">results&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">qubit1&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">qubit2&lt;/span>&lt;span class="o">.&lt;/span> &lt;span class="n">If&lt;/span> &lt;span class="n">it&lt;/span> &lt;span class="n">is&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">Bell&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">they&lt;/span> &lt;span class="n">will&lt;/span> &lt;span class="n">always&lt;/span> &lt;span class="n">match&lt;/span>&lt;span class="o">.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">operation&lt;/span> &lt;span class="n">GenerateAndMeasureBellState&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">Result&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Result&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Allocate&lt;/span> &lt;span class="n">two&lt;/span> &lt;span class="n">qubits&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">initial&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="n">is&lt;/span> &lt;span class="n">automatically&lt;/span> &lt;span class="o">|&lt;/span>&lt;span class="mi">00&lt;/span>&lt;span class="o">&amp;gt;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">use&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">q1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">q2&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">Qubit&lt;/span>&lt;span class="p">(),&lt;/span> &lt;span class="n">Qubit&lt;/span>&lt;span class="p">());&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Apply&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">Hadamard&lt;/span> &lt;span class="n">gate&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">q1&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">create&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">superposition&lt;/span> &lt;span class="n">state&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">H&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">q1&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Apply&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">CNOT&lt;/span> &lt;span class="n">gate&lt;/span> &lt;span class="n">with&lt;/span> &lt;span class="n">q1&lt;/span> &lt;span class="n">as&lt;/span> &lt;span class="n">control&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">q2&lt;/span> &lt;span class="n">as&lt;/span> &lt;span class="n">target&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">This&lt;/span> &lt;span class="n">generates&lt;/span> &lt;span class="n">quantum&lt;/span> &lt;span class="n">entanglement&lt;/span> &lt;span class="n">between&lt;/span> &lt;span class="n">q1&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">q2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">CNOT&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">q1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">q2&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">As&lt;/span> &lt;span class="n">a&lt;/span> &lt;span class="n">debug&lt;/span> &lt;span class="n">step&lt;/span> &lt;span class="n">during&lt;/span> &lt;span class="n">development&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">you&lt;/span> &lt;span class="n">can&lt;/span> &lt;span class="n">dump&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="n">vector&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">simulator&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">verify&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">DumpMachine&lt;/span>&lt;span class="p">();&lt;/span> &lt;span class="o">//&lt;/span> &lt;span class="n">Uncomment&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">necessary&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Measure&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">simultaneously&lt;/span> &lt;span class="n">reset&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="o">|&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="o">&amp;gt;&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">safely&lt;/span> &lt;span class="n">release&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">qubits&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">let&lt;/span> &lt;span class="n">res1&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">MResetZ&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">q1&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">let&lt;/span> &lt;span class="n">res2&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">MResetZ&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">q2&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Return&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">pair&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">measurement&lt;/span> &lt;span class="n">results&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">res1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">res2&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1"># Summary&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="n">The&lt;/span> &lt;span class="n">main&lt;/span> &lt;span class="n">routine&lt;/span> &lt;span class="n">that&lt;/span> &lt;span class="n">executes&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">Bell&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="n">generation&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">measurement&lt;/span> &lt;span class="n">experiment&lt;/span> &lt;span class="n">multiple&lt;/span> &lt;span class="n">times&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">collects&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">statistics&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">results&lt;/span>&lt;span class="o">.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1"># Input&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1">## count&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="n">The&lt;/span> &lt;span class="n">number&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">times&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">repeat&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">experiment&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">e&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">g&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">1000&lt;/span> &lt;span class="n">times&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="c1"># Output&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">///&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">):&lt;/span> &lt;span class="n">The&lt;/span> &lt;span class="n">number&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">times&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mi">00&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">01&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">11&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="n">were&lt;/span> &lt;span class="n">observed&lt;/span> &lt;span class="n">respectively&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="err">@&lt;/span>&lt;span class="n">EntryPoint&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">operation&lt;/span> &lt;span class="n">RunBellStateExperiment&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">count&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">Int&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Initialize&lt;/span> &lt;span class="n">mutable&lt;/span> &lt;span class="n">variables&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">count&lt;/span> &lt;span class="n">observations&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">mutable&lt;/span> &lt;span class="n">num00&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">mutable&lt;/span> &lt;span class="n">num01&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">mutable&lt;/span> &lt;span class="n">num10&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">mutable&lt;/span> &lt;span class="n">num11&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Run&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">experiment&lt;/span> &lt;span class="n">loop&lt;/span> &lt;span class="k">for&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">specified&lt;/span> &lt;span class="n">count&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">for&lt;/span> &lt;span class="n">_&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="mf">1.&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">count&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Generate&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">Bell&lt;/span> &lt;span class="n">state&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">receive&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">measurement&lt;/span> &lt;span class="n">results&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">let&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">r1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">r2&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">GenerateAndMeasureBellState&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Increment&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">count&lt;/span> &lt;span class="k">for&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">resulting&lt;/span> &lt;span class="n">pattern&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">r1&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">Zero&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">r2&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">Zero&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">set&lt;/span> &lt;span class="n">num00&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span> &lt;span class="k">elif&lt;/span> &lt;span class="n">r1&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">Zero&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">r2&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">One&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">set&lt;/span> &lt;span class="n">num01&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span> &lt;span class="k">elif&lt;/span> &lt;span class="n">r1&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">One&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">r2&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">Zero&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">set&lt;/span> &lt;span class="n">num10&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="o">//&lt;/span> &lt;span class="n">In&lt;/span> &lt;span class="k">case&lt;/span> &lt;span class="n">of&lt;/span> &lt;span class="n">r1&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">One&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">r2&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">One&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">set&lt;/span> &lt;span class="n">num11&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">//&lt;/span> &lt;span class="n">Output&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">gathered&lt;/span> &lt;span class="n">statistics&lt;/span> &lt;span class="n">as&lt;/span> &lt;span class="n">messages&lt;/span> &lt;span class="n">to&lt;/span> &lt;span class="n">the&lt;/span> &lt;span class="n">console&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;--- Experiment Results ---&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;Total runs: {count}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;00 observed: {num00}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;01 observed: {num01}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;10 observed: {num10}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Message&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">$&lt;/span>&lt;span class="s2">&amp;#34;11 observed: {num11}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">num00&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">num01&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">num10&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">num11&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="code-explanation-and-execution-verification">Code Explanation and Execution Verification
&lt;/h3>&lt;ul>
&lt;li>&lt;code>namespace&lt;/code>: Similar to Java or C#, this is a namespace declaration to logically organize programs and prevent naming collisions.&lt;/li>
&lt;li>&lt;code>open&lt;/code>: Imports required libraries (modules). &lt;code>Microsoft.Quantum.Intrinsic&lt;/code> includes basic quantum gates like H, X, Y, Z, and CNOT, while &lt;code>Microsoft.Quantum.Measurement&lt;/code> contains convenient measurement-related features like &lt;code>MResetZ&lt;/code>.&lt;/li>
&lt;li>&lt;code>use (q1, q2) = (Qubit(), Qubit());&lt;/code>: Dynamically allocates two qubits.&lt;/li>
&lt;li>&lt;code>H(q1); CNOT(q1, q2);&lt;/code>: These two lines are the very core that generates quantum entanglement. It can be written very simply and intuitively.&lt;/li>
&lt;li>&lt;code>let res1 = MResetZ(q1);&lt;/code>: As mentioned earlier, &lt;code>MResetZ&lt;/code> binds the measurement result to a variable while forcibly resetting the qubit&amp;rsquo;s state to $|0\rangle$. This allows the qubits to be safely released at the end of the &lt;code>use&lt;/code> block.&lt;/li>
&lt;li>&lt;code>@EntryPoint()&lt;/code>: Adding this attribute indicates to the compiler that this operation is the starting point of the program&amp;rsquo;s execution (much like the main function in C).&lt;/li>
&lt;/ul>
&lt;p>In theory, since the generated state is the Bell state $\frac{1}{\sqrt{2}}(|00\rangle + |11\rangle)$, if you run this program enough times (say, 10,000 times), you should observe &lt;code>00&lt;/code> and &lt;code>11&lt;/code> roughly 50% of the time each (around 5,000 times), and &lt;code>01&lt;/code> and &lt;code>10&lt;/code> will be 0 times (not observed at all, barring theoretical error). This serves as proof that the two qubits have a strong correlation (are entangled).&lt;/p>
&lt;h2 id="7-seamless-integration-with-host-languages-python--c">7. Seamless Integration with Host Languages (Python / C#)
&lt;/h2>&lt;p>While Q# can be run standalone by specifying an &lt;code>@EntryPoint()&lt;/code> as in the previous example (a Q# standalone application), in actual enterprise development or research use cases, it is used closely integrated with classical processing, such as GUI front-ends, fetching data from massive databases, and machine learning optimization loops (like parameter updates for VQE). Therefore, Q# offers sophisticated Interoperability, allowing it to be effortlessly called and executed directly from host languages like Python or C# (.NET).&lt;/p>
&lt;h3 id="71-example-of-calling-from-python-for-data-scientists">7.1 Example of Calling from Python: For Data Scientists
&lt;/h3>&lt;p>To call Q# from Python, which holds a dominant share in the worlds of data science, machine learning, and physics research, you use the &lt;code>qsharp&lt;/code> Python package. It also has high affinity with Jupyter Notebooks, making it perfect to combine with interactive development and data visualization.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 1. Import the necessary Q# integration module&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">qsharp&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 2. Import Q# operations directly as if they were Python functions&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># (The compiler automatically handles binding and compilation in the background)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">Quantum.BellState&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">RunBellStateExperiment&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># 3. Call and execute it from a Python script (using the simulator)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">count&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">1000&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;Starting quantum simulation for &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">count&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> iterations...&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Calling the simulate() method executes it on the local simulator&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">result&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">RunBellStateExperiment&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">simulate&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">count&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">count&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Receive the result tuple, format it on the Python side, and output&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="se">\n&lt;/span>&lt;span class="s2">--- Simulation Results ---&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;|00&amp;gt; : &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">result&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> (Expected ~500)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;|01&amp;gt; : &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">result&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> (Expected 0)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;|10&amp;gt; : &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">result&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> (Expected 0)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">print&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;|11&amp;gt; : &lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">result&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">3&lt;/span>&lt;span class="p">]&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2"> (Expected ~500)&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>In this way, because the Q# compiler and interpreter transparently generate bindings on the fly via C APIs in the background, you can treat quantum algorithms as just another black-box function from the Python code side, allowing you to easily build classical-quantum hybrid algorithms.&lt;/p>
&lt;h3 id="72-example-of-calling-from-c-for-enterprise-development">7.2 Example of Calling from C#: For Enterprise Development
&lt;/h3>&lt;p>You can integrate Q# code just as seamlessly from C#, which is powerful in large-scale backend systems and enterprise application development. By placing a Q# project (.csproj) and a C# project within the same solution and creating a reference relationship, C# class wrappers are automatically generated at build time.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;span class="lnt">23
&lt;/span>&lt;span class="lnt">24
&lt;/span>&lt;span class="lnt">25
&lt;/span>&lt;span class="lnt">26
&lt;/span>&lt;span class="lnt">27
&lt;/span>&lt;span class="lnt">28
&lt;/span>&lt;span class="lnt">29
&lt;/span>&lt;span class="lnt">30
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-csharp" data-lang="csharp">&lt;span class="line">&lt;span class="cl">&lt;span class="k">using&lt;/span> &lt;span class="nn">System&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">using&lt;/span> &lt;span class="nn">System.Threading.Tasks&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">using&lt;/span> &lt;span class="nn">Microsoft.Quantum.Simulation.Simulators&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="c1">// Namespace for the quantum simulator&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">using&lt;/span> &lt;span class="nn">Quantum.BellState&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="c1">// Namespace defined in Q#&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">namespace&lt;/span> &lt;span class="nn">QuantumRunner&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">class&lt;/span> &lt;span class="nc">Program&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kd">static&lt;/span> &lt;span class="kd">async&lt;/span> &lt;span class="n">Task&lt;/span> &lt;span class="n">Main&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="kt">string&lt;/span>&lt;span class="p">[]&lt;/span> &lt;span class="n">args&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Create an instance of the full-state quantum simulator&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Since it implements IDisposable, we manage resources properly with a using statement&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">using&lt;/span> &lt;span class="nn">var&lt;/span> &lt;span class="n">sim&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="k">new&lt;/span> &lt;span class="n">QuantumSimulator&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">long&lt;/span> &lt;span class="n">count&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="m">1000&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">WriteLine&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">$&amp;#34;Running {count} iterations of Bell State generation...&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Execute the Q# operation asynchronously. The Run method is auto-generated.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// Pass &amp;#39;sim&amp;#39; as the execution target, and &amp;#39;count&amp;#39; as the argument.&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">var&lt;/span> &lt;span class="n">result&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="k">await&lt;/span> &lt;span class="n">RunBellStateExperiment&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">Run&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">sim&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">count&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// The result is returned as a C# ValueTuple&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">WriteLine&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">$&amp;#34;|00&amp;gt;: {result.Item1}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">WriteLine&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">$&amp;#34;|01&amp;gt;: {result.Item2}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">WriteLine&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">$&amp;#34;|10&amp;gt;: {result.Item3}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">Console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">WriteLine&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s">$&amp;#34;|11&amp;gt;: {result.Item4}&amp;#34;&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Here, we are using the local &lt;code>QuantumSimulator&lt;/code> class for development purposes, but when moving to a production environment, you simply need to replace the instantiation part of this simulator with a cloud target provider pointing to an Azure Quantum workspace (for instance, an IonQ or Quantinuum machine object). Without changing any of the Q# code or business logic, it becomes possible to execute the algorithm on actual quantum hardware in the cloud. This is the true power of the QDK.&lt;/p>
&lt;h2 id="8-advanced-topics-feature-sets-embodying-qs-design-philosophy">8. Advanced Topics: Feature Sets Embodying Q#&amp;rsquo;s Design Philosophy
&lt;/h2>&lt;p>We have seen the basic usage of Q#, but now let&amp;rsquo;s dive a bit deeper into the more advanced features of Q# and the design philosophy behind them. It is these features that make Q# a true quantum domain-specific language, rather than just a &amp;ldquo;Python alternative&amp;rdquo;.&lt;/p>
&lt;h3 id="81-automatic-generation-of-adjoint-and-controlled-operations">8.1 Automatic Generation of Adjoint and Controlled Operations
&lt;/h3>&lt;p>One of the major characteristics of quantum computation is &amp;ldquo;Reversibility&amp;rdquo;, which stems from Unitarity. All basic operations except for measurements are unitary matrices, and therefore invariably have inverse matrices (inverse operations) and can be reversed. Q# provides powerful functor modifiers, &lt;code>Adjoint&lt;/code> and &lt;code>Controlled&lt;/code>, which support this as a first-class language feature.&lt;/p>
&lt;p>For a given quantum operation &lt;code>Op&lt;/code>, instead of manually calculating the matrix or reversing the sequence of gates to implement its inverse operation, you can just add specific keywords to the function&amp;rsquo;s signature, and the Q# compiler will automatically generate &lt;code>Adjoint Op&lt;/code> for you. Similarly, a conditional operation &lt;code>Controlled Op&lt;/code>, which executes &lt;code>Op&lt;/code> only when a specific group of qubits are all $|1\rangle$, can also be auto-generated.&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;span class="lnt">11
&lt;/span>&lt;span class="lnt">12
&lt;/span>&lt;span class="lnt">13
&lt;/span>&lt;span class="lnt">14
&lt;/span>&lt;span class="lnt">15
&lt;/span>&lt;span class="lnt">16
&lt;/span>&lt;span class="lnt">17
&lt;/span>&lt;span class="lnt">18
&lt;/span>&lt;span class="lnt">19
&lt;/span>&lt;span class="lnt">20
&lt;/span>&lt;span class="lnt">21
&lt;/span>&lt;span class="lnt">22
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">// By appending &amp;#39;is Adj + Ctl&amp;#39;, you instruct the compiler to auto-generate the inverse and controlled operations
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">operation MyComplexSubroutine(qubits: Qubit[]) : Unit is Adj + Ctl {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // Write a highly complex sequence of quantum gates here
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // e.g.: combinations of H, T, CNOT, arbitrary phase shifts, etc.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">// Example of caller side
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">operation UseMyOp(controlQubit: Qubit, targetQubits: Qubit[]) : Unit {
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // Normal call
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> MyComplexSubroutine(targetQubits);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // Executing the inverse operation: completely rewinding back to the original state (very useful for uncomputation)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> Adjoint MyComplexSubroutine(targetQubits);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // Executing the controlled operation: run the complex subroutine only when controlQubit is |1&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> Controlled MyComplexSubroutine([controlQubit], targetQubits);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> // Furthermore, combinations like the inverse of a controlled operation are also possible!
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> Controlled Adjoint MyComplexSubroutine([controlQubit], targetQubits);
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Thanks to this feature, implementing advanced algorithms that frequently use complex subroutines and their inverse operations (Uncomputation, used to disentangle unnecessary correlations) — such as the oracle implementation of Grover&amp;rsquo;s search algorithm or Shor&amp;rsquo;s factoring algorithm — is dramatically simplified, greatly reducing the room for human error and bugs. Compared to circuit-building models like Qiskit, this can be considered one of the biggest strengths of Q# as an algorithm description language.&lt;/p>
&lt;h3 id="82-resource-estimation-and-preparing-for-the-future">8.2 Resource Estimation and Preparing for the Future
&lt;/h3>&lt;p>Current quantum computers are in a developmental stage called &amp;ldquo;NISQ (Noisy Intermediate-Scale Quantum)&amp;rdquo;, where the number of available qubits is small (tens to hundreds) and error rates are high. However, looking ahead to the era of Fault-Tolerant Quantum Computers (FTQC), to run a new algorithm, it becomes critically important to accurately estimate in advance: &amp;ldquo;Exactly how many logical qubits are needed?&amp;rdquo;, &amp;ldquo;How many times will T gates or Toffoli gates, which are very costly in error correction, be used?&amp;rdquo;, and &amp;ldquo;How long will the execution take?&amp;rdquo;&lt;/p>
&lt;p>The QDK incorporates a &amp;ldquo;Resource Estimator&amp;rdquo; as one of its execution environment targets. By using this, rather than running the code on an actual machine or a heavy full-state simulator, you can analyze the logical paths of the code to instantly calculate and output the resource requirements of large-scale algorithms. This allows algorithm designers and researchers to rapidly iterate on optimizations at the specific gate count level, not just theoretical complexity, even for future algorithms requiring thousands or tens of thousands of qubits.&lt;/p>
&lt;h2 id="9-conclusion-expectations-for-next-generation-software-engineers">9. Conclusion: Expectations for Next-Generation Software Engineers
&lt;/h2>&lt;p>Quantum computing is rapidly transitioning from purely theoretical concepts that once resided in the minds of physicists like Einstein, Schrödinger, and Feynman, into a concrete domain of engineering implementation accessible to anyone in the world via browsers and command lines through cloud infrastructure (Azure Quantum, AWS Braket, IBM Quantum, etc.). The speed of hardware evolution is staggering, and many experts predict that the day we prove &amp;ldquo;useful&amp;rdquo; quantum advantage will arrive within a few years.&lt;/p>
&lt;p>The Microsoft Q# language introduced in this article beautifully brought the excellent practices cultivated over decades in the classical programming world (strong typing, functional programming elements, modularization, encapsulation, and advanced IDE support) into the entirely new world of quantum programming. In the process of learning Q# and implementing quantum algorithms, we can gain deep insights that return to the roots of computer science and physics, such as &amp;ldquo;What is state?&amp;rdquo;, &amp;ldquo;What is measurement?&amp;rdquo;, and &amp;ldquo;How does information propagate through space?&amp;rdquo;. This is an experience accompanied by tremendous intellectual excitement that transcends mere skill improvement.&lt;/p>
&lt;p>In the near future, just as current machine learning engineers naturally draw out the parallel computing power of GPUs using PyTorch and TensorFlow, the era will surely come where next-generation &amp;ldquo;quantum software engineers&amp;rdquo; use Q# and Qiskit to unleash the transcendental computing power of QPUs (Quantum Processing Units) to tackle massive human-scale challenges: discovering new materials via materials science, molecular simulation in drug discovery, climate change modeling, and risk optimization in finance.&lt;/p>
&lt;p>For software developers currently focusing mainly on classical web applications, mobile apps, or data analysis, I encourage you to take this opportunity to step into the world of quantum programming. At first, you might be bewildered by phenomena unique to quantum mechanics that defy intuition (superposition, entanglement, probabilistic behavior). However, the refined dedicated language that is Q# and the powerful toolchain that is the QDK will surely and strongly support your learning curve.&lt;/p>
&lt;h2 id="10-reference-links-for-deeper-learning">10. Reference Links for Deeper Learning
&lt;/h2>&lt;p>Here are some excellent resources to continue your quantum programming journey.&lt;/p>
&lt;ul>
&lt;li>&lt;a class="link" href="https://learn.microsoft.com/azure/quantum/" target="_blank" rel="noopener"
>Microsoft Azure Quantum Official Documentation&lt;/a> : A comprehensive documentation portal for QDK and Azure Quantum.&lt;/li>
&lt;li>&lt;a class="link" href="https://learn.microsoft.com/azure/quantum/user-guide/" target="_blank" rel="noopener"
>Q# User Guide and Reference&lt;/a> : A complete reference for Q#&amp;rsquo;s syntax, type system, and standard libraries.&lt;/li>
&lt;li>&lt;a class="link" href="https://quantum.microsoft.com/en-us/experience/quantum-katas" target="_blank" rel="noopener"
>Quantum Katas&lt;/a> : A collection of open-source tutorials provided by Microsoft. It is a fantastic resource where you can interactively self-study the basic concepts of quantum computing (quantum gates, measurements, algorithm construction) by actually writing Q# code in a Test-Driven Development (TDD) format.&lt;/li>
&lt;li>&lt;a class="link" href="https://github.com/microsoft/qsharp-compiler" target="_blank" rel="noopener"
>Q# GitHub Repository&lt;/a> : The Q# language compiler and standard library themselves are actively developed as open source. A must-see for those interested in the internal structure of the compiler.&lt;/li>
&lt;/ul>
&lt;p>The future of quantum computing has just begun and is full of infinite possibilities. I invite you to embrace a mindset that enjoys new programming paradigms, and by all means, challenge yourself with programming in Q#!&lt;/p></description></item></channel></rss>