├── ComplexSimulation.qs ├── LICENSE ├── PauliZ.qs ├── Q.js ├── Quantum-buffers.qs ├── QuantumTeleportation.qs ├── README.md └── sample.qs /ComplexSimulation.qs: -------------------------------------------------------------------------------- 1 | namespace Quantum.ComplexSimulation { 2 | open Microsoft.Quantum.Canon; 3 | open Microsoft.Quantum.Intrinsic; 4 | open Microsoft.Quantum.Measurement; 5 | 6 | // Quantum Fourier Transform (QFT) - A key part of the algorithm for frequency analysis 7 | operation QFT(qubits: Qubit[]) : Unit { 8 | let n = Length(qubits); 9 | 10 | for (i in 0..n-1) { 11 | // Apply Hadamard gate to each qubit 12 | H(qubits[i]); 13 | 14 | for (j in i+1..n-1) { 15 | // Apply controlled rotation for each qubit pair 16 | let angle = 1.0 / (2.0 ^ (j - i + 1)); 17 | CR(qubits[j], qubits[i], angle * PI); 18 | } 19 | } 20 | 21 | // Apply final Hadamard gate 22 | ApplyToEach(H, qubits); 23 | 24 | // Reverse the qubit order (to make the result readable) 25 | ApplyToEach(X, qubits); 26 | } 27 | 28 | // Quantum Simulation of a complex system with differential equations (e.g., Schrödinger equation) 29 | operation QuantumDifferentialEquationSolver() : Unit { 30 | // Simulating a simple quantum system (Schrödinger equation) 31 | mutable qubits = Qubit[5]; // Number of qubits for the system state 32 | 33 | using (qubits) { 34 | // Prepare the system in a superposition state 35 | ApplyToEach(H, qubits); 36 | 37 | // Simulate a quantum evolution (solving the Schrödinger equation) 38 | for (i in 0..4) { 39 | // Apply a series of rotations to simulate evolution 40 | let angle = 2.0 * PI / (2.0 ^ (i + 1)); 41 | Rz(angle, qubits[i]); // Rotation around the Z-axis 42 | Ry(angle, qubits[i]); // Rotation around the Y-axis 43 | } 44 | 45 | // Perform a Quantum Fourier Transform to analyze the frequencies of the system 46 | QFT(qubits); 47 | 48 | // Measure the qubits to observe the final state 49 | let result = M(qubits[0]); 50 | if (result == Zero) { 51 | Message("The system evolved into state 0."); 52 | } else { 53 | Message("The system evolved into state 1."); 54 | } 55 | } 56 | } 57 | 58 | // Main function to run the quantum system simulation 59 | operation RunComplexQuantumSimulation() : Unit { 60 | // Run the quantum differential equation solver simulation 61 | QuantumDifferentialEquationSolver(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Pourya Bagheri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PauliZ.qs: -------------------------------------------------------------------------------- 1 | @EntryPoint() 2 | operation MeasureOneQubit() : Result { 3 | // The following using block creates a fresh qubit and initializes it 4 | // in the |0〉 state. 5 | use qubit = Qubit(); 6 | // We apply a Hadamard operation H to the state, thereby preparing the 7 | // state 1 / sqrt(2) (|0〉 + |1〉). 8 | H(qubit); 9 | // Now we measure the qubit in Z-basis. 10 | let result = M(qubit); 11 | // As the qubit is now in an eigenstate of the measurement operator, 12 | // we reset the qubit before releasing it. 13 | if result == One { X(qubit); } 14 | // Finally, we return the result of the measurement. 15 | return result; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Q.js: -------------------------------------------------------------------------------- 1 | // Free and open-source 2 | // Q is free to use, our code is open-source, and our API is heavily documented. Still a quantum novice? 3 | // Each page of API documentation includes simple explanations of basic quantum concepts to get you up to speed quickly. 4 | // This makes Q ideal for the classroom as well as autodidacts at home. Q just might be the most accessible quantum circuit 5 | // suite in the world. Join our project on GitHub at https://github.com/stewdio/q.js and drop a link to Q’s website 6 | // https://quantumjavascript.app on social media with the hashtag #Qjs. Let’s make quantum computing accessible! 7 | 8 | 9 | 10 | // Quantum JavaScript 11 | // What does coding a quantum circuit look like? Let’s recreate the above Bell state using three separate circuit authoring 12 | // styles to demonstrate Q’s flexibility. For each of the three examples we’ll create a circuit that uses 2 qubit registers 13 | // for 2 moments of time. We’ll place a Hadamard gate at moment 1 on register 1. Then we’ll place a Controlled-Not gate at 14 | // moment 2, with its control component on register 1 and its target component on register 2. 15 | 16 | // # 17 | // 1. Text as input 18 | // Q’s text-as-input feature directly converts your text into a functioning quantum circuit. Just type 19 | // your operations out as if creating a text-only circuit diagram (using “I” for identity gates in the spots where 20 | // no operations occur) and enclose your text block in backticks (instead of quotation marks). Note that parentheses are 21 | // not required to invoke the function call when using backticks. 22 | 23 | 24 | Q` 25 | H X#0 26 | I X#1 27 | ` 28 | 29 | 30 | Q`H`.try$() ? 'tails' : 'heads' 31 | -------------------------------------------------------------------------------- /Quantum-buffers.qs: -------------------------------------------------------------------------------- 1 | // Explanation: 2 | // 1. Buffer Allocation: 3 | // AllocateBuffer dynamically creates an array of qubits to represent a quantum buffer. A message logs the allocation. 4 | // 5 | // 2. Buffer Usage: 6 | // UseBuffer applies a set of operations (e.g., Hadamard gates) to all qubits in the buffer, simulating the use of allocated quantum memory. 7 | // 8 | // 3. Garbage Collection: 9 | // FreeBuffer ensures all qubits are reset to their ground state before deallocation to prevent quantum garbage from affecting future computations. 10 | // 11 | // 4. Management Workflow: 12 | // ManageBuffersAndGarbageCollection integrates allocation, usage, and deallocation for comprehensive buffer management. 13 | // 14 | // 5. Entry Point: 15 | // The Main operation demonstrates the process, initializing a buffer size and invoking the management routine. 16 | 17 | // Namespace and Imports 18 | // namespace Quantum.BufferManagement 19 | // Defines the logical scope for the code, allowing organization of related operations under a single namespace. 20 | namespace Quantum.BufferManagement { 21 | 22 | // Provides access to intrinsic quantum operations, like H (Hadamard gate) and Reset. 23 | open Microsoft.Quantum.Intrinsic; 24 | 25 | 26 | // Includes canonical quantum operations and higher-level abstractions often used in quantum programs. 27 | open Microsoft.Quantum.Canon; 28 | 29 | // Declares an operation to allocate a quantum buffer of size qubits, returning an array of Qubit. 30 | operation AllocateBuffer(size : Int) : Qubit[] { 31 | 32 | // Allocates an array of size qubits, which will be automatically deallocated when the use block ends. 33 | // Allocate a quantum buffer of given size 34 | use qubits = Qubit[size]; 35 | 36 | // Outputs a message to log the allocation for debugging or monitoring purposes. 37 | Message($"Buffer of size {size} allocated."); 38 | 39 | // Returns the allocated buffer (array of qubits) to the caller. 40 | return qubits; 41 | } 42 | 43 | // Declares an operation to perform quantum computations on the given buffer of qubits. 44 | operation UseBuffer(qubits : Qubit[]) : Unit { 45 | // Apply some quantum operations to the buffer 46 | for (q in qubits) { 47 | 48 | // Applies the Hadamard gate to the current qubit, placing it into an equal superposition state. 49 | H(q); // Apply Hadamard gate to each qubit 50 | 51 | } 52 | 53 | // Logs a message indicating that operations were successfully applied to all qubits in the buffer. 54 | Message($"Operations applied to the buffer."); 55 | } 56 | 57 | // Declares an operation to safely deallocate the quantum buffer by resetting all qubits. 58 | operation FreeBuffer(qubits : Qubit[]) : Unit { 59 | // Deallocate the quantum buffer 60 | for (q in qubits) { 61 | 62 | // Ensures that the qubit is reset to the ground state (∣0⟩∣0⟩) before deallocation. 63 | // This is critical for avoiding quantum garbage. 64 | Reset(q); 65 | } 66 | 67 | // Logs the size of the buffer and confirms that it has been freed. 68 | Message($"Buffer of size {Length(qubits)} freed."); 69 | } 70 | 71 | // A higher-level operation that orchestrates allocation, use, and deallocation of quantum buffers. 72 | operation ManageBuffersAndGarbageCollection(size : Int) : Unit { 73 | 74 | // Calls AllocateBuffer to create a buffer of size qubits and assigns it to a mutable variable buffer. 75 | // Main operation to manage buffers and garbage collection 76 | mutable buffer = AllocateBuffer(size); 77 | 78 | // Calls UseBuffer to perform operations on the allocated buffer. 79 | UseBuffer(buffer); 80 | 81 | // Calls FreeBuffer to reset and deallocate the buffer, ensuring no quantum garbage is left. 82 | FreeBuffer(buffer); 83 | } 84 | 85 | // Marks the Main operation as the starting point of the program when executed. 86 | @EntryPoint() 87 | 88 | // Declares the main entry operation for the program. 89 | operation Main() : Unit { 90 | 91 | // Sets the buffer size to 5 qubits for this example. 92 | let bufferSize = 5; 93 | 94 | // Calls the ManageBuffersAndGarbageCollection operation with the defined buffer size to demonstrate the workflow. 95 | ManageBuffersAndGarbageCollection(bufferSize); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /QuantumTeleportation.qs: -------------------------------------------------------------------------------- 1 | 2 | // This algorithm allows the quantum state of a qubit to be transmitted between two parties (commonly called Alice and Bob) using classical communication and quantum entanglement. 3 | 4 | // Detailed Documentation 5 | // Operation: QuantumTeleportation 6 | // Summary: This operation demonstrates quantum teleportation, transferring the quantum state of one qubit (qubit1) to another qubit (qubit3) using classical communication and quantum entanglement. 7 | // Parameters: 8 | // alpha: A Double representing the amplitude of the |0⟩ state of the initial qubit. 9 | // beta: A Double representing the amplitude of the |1⟩ state of the initial qubit. 10 | // Returns: A tuple of three results (Result, Result, Result): 11 | // First two results are the classical bits representing Alice's measurements. 12 | // The third result is the measurement of the final teleported state at Bob's qubit. 13 | 14 | // Steps: 15 | // Allocate three qubits: one for the initial quantum state (qubit1), one for Alice (qubit2), and one for Bob (qubit3). 16 | // Prepare qubit1 in a superposition of |0⟩ and |1⟩ based on alpha and beta. 17 | // Entangle qubit2 (Alice) and qubit3 (Bob) using a Hadamard gate and CNOT gate. 18 | // Perform a Bell-state measurement on qubit1 and qubit2. 19 | // Based on the measurement results, apply corrective gates (X, Z) to Bob's qubit (qubit3). 20 | // Measure Bob’s qubit (qubit3) to verify the teleported state. 21 | // Operation: PrepareQubit 22 | // Summary: Prepares a qubit in the quantum state alpha|0⟩ + beta|1⟩. 23 | // Parameters: 24 | // alpha: Amplitude of the |0⟩ state. 25 | // beta: Amplitude of the |1⟩ state. 26 | // qubit: The qubit to prepare. 27 | // Operation: ResetQubit 28 | // Summary: Ensures that a qubit is reset to the |0⟩ state. 29 | // Parameters: 30 | // qubit: The qubit to reset. 31 | // Operation: ResetAll 32 | // Summary: Resets all qubits in an array to the |0⟩ state before releasing them. 33 | // Parameters: 34 | // qubits: An array of qubits to reset. 35 | // Explanation: 36 | // This code demonstrates the process of quantum teleportation, where the state of one qubit is transferred to another without directly interacting with it. 37 | // The quantum entanglement and measurement process ensures that the qubit at Bob's end receives the teleported state. 38 | // In real-world quantum computing, this process is crucial for quantum communication and secure information transmission. 39 | open Microsoft.Quantum.Intrinsic; 40 | open Microsoft.Quantum.Canon; 41 | open Microsoft.Quantum.Measurement; 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | /// Summary 51 | /// This operation demonstrates the quantum teleportation algorithm. 52 | /// It takes as input an initial quantum state (given by parameters alpha and beta), 53 | /// and teleports this state from qubit1 (Alice) to qubit3 (Bob). 54 | /// 55 | /// Inputs 56 | /// - alpha: The amplitude of the |0⟩ state of the qubit. 57 | /// - beta: The amplitude of the |1⟩ state of the qubit. 58 | /// 59 | /// Outputs 60 | /// The operation returns the measurement result for the two classical bits communicated 61 | /// between Alice and Bob, as well as the final teleported state. 62 | operation QuantumTeleportation(alpha : Double, beta : Double) : (Result, Result, Result) { 63 | mutable resultAlice = Zero; 64 | mutable resultBob = Zero; 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | // Step 1: Allocate three qubits. 74 | // qubit1: The qubit Alice wishes to teleport. 75 | // qubit2: Alice's part of the entangled pair. 76 | // qubit3: Bob's part of the entangled pair. 77 | use (qubit1, qubit2, qubit3) = (Qubit(), Qubit(), Qubit()); 78 | 79 | 80 | 81 | 82 | 83 | 84 | // Step 2: Prepare the initial state of qubit1 (the state to be teleported). 85 | // The qubit is prepared in the state alpha|0⟩ + beta|1⟩. 86 | PrepareQubit(alpha, beta, qubit1); 87 | 88 | 89 | 90 | 91 | 92 | 93 | // Step 3: Create an entangled pair between qubit2 (Alice) and qubit3 (Bob). 94 | H(qubit2); 95 | CNOT(qubit2, qubit3); 96 | 97 | 98 | 99 | 100 | 101 | 102 | // Step 4: Perform Bell-state measurement on qubit1 and qubit2. 103 | CNOT(qubit1, qubit2); 104 | H(qubit1); 105 | 106 | 107 | 108 | 109 | 110 | 111 | // Step 5: Measure both qubits and store the results. 112 | set resultAlice = M(qubit1); 113 | set resultBob = M(qubit2); 114 | 115 | 116 | 117 | 118 | 119 | 120 | // Step 6: Based on Alice's measurement results, apply corrections to qubit3. 121 | if (resultBob == One) { 122 | X(qubit3); 123 | } 124 | if (resultAlice == One) { 125 | Z(qubit3); 126 | } 127 | 128 | 129 | 130 | 131 | 132 | // Step 7: Measure Bob's qubit (qubit3) to verify the teleported state. 133 | let resultFinal = M(qubit3); 134 | 135 | 136 | 137 | 138 | 139 | // Reset all qubits before releasing them. 140 | ResetAll([qubit1, qubit2, qubit3]); 141 | 142 | 143 | 144 | 145 | 146 | // Return the results of Alice's measurements and the final measurement of Bob's qubit. 147 | return (resultAlice, resultBob, resultFinal); 148 | } 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | /// Summary 158 | /// This operation prepares a qubit in a specified quantum state. 159 | /// 160 | /// Inputs 161 | /// - alpha: The amplitude of the |0⟩ state. 162 | /// - beta: The amplitude of the |1⟩ state. 163 | /// - qubit: The qubit to be prepared. 164 | /// 165 | /// Remarks 166 | /// The state prepared is of the form `alpha|0⟩ + beta|1⟩`. 167 | operation PrepareQubit(alpha : Double, beta : Double, qubit : Qubit) : Unit { 168 | // Ensure that alpha^2 + beta^2 = 1, which is necessary for valid quantum states. 169 | let norm = Sqrt(alpha * alpha + beta * beta); 170 | let alphaNormalized = alpha / norm; 171 | let betaNormalized = beta / norm; 172 | // Apply the state preparation. 173 | if (betaNormalized != 0.0) { 174 | RY(2.0 * ArcTan2(betaNormalized, alphaNormalized), qubit); 175 | } 176 | } 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | /// Summary 188 | /// Resets a qubit to the |0⟩ state if it is not already in that state. 189 | /// 190 | /// Inputs 191 | /// - qubit: The qubit to be reset. 192 | /// 193 | /// Remarks 194 | /// Resetting ensures qubits are returned to the |0⟩ state before deallocation. 195 | operation ResetQubit(qubit : Qubit) : Unit { 196 | let result = M(qubit); 197 | if result == One { 198 | X(qubit); 199 | } 200 | } 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | /// Summary 209 | /// Resets an array of qubits to the |0⟩ state. 210 | /// 211 | /// Inputs 212 | /// - qubits: The array of qubits to reset. 213 | /// 214 | /// Remarks 215 | /// This operation ensures all qubits in the array are set to |0⟩ before deallocation. 216 | operation ResetAll(qubits : Qubit[]) : Unit { 217 | for qubit in qubits { 218 | ResetQubit(qubit); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quantum-computer-programming 2 | Research and development of coding in the field of emerging quantum computers. 3 | 4 | 5 | What are the Q# programming language and Quantum Development Kit (QDK)? 6 | ---- 7 | Q# is Microsoft’s open-source programming language for developing and running quantum algorithms. It’s part of the Quantum Development Kit (QDK), which includes Q# libraries, quantum simulators, extensions for other programming environments, and API documentation. In addition to the Standard Q# library, the QDK includes Chemistry, Machine Learning, and Numeric libraries. 8 | 9 | As a programming language, Q# draws familiar elements from Python, C#, and F# and supports a basic procedural model for writing programs with loops, if/then statements, and common data types. It also introduces new quantum-specific data structures and operations. 10 | 11 | 12 | 13 | How does Q# work? 14 | --- 15 | A Q# program can compile into a standalone application or be called by a host program that is written either in Python or a .NET language. 16 | 17 | When you compile and run the program, it creates an instance of the quantum simulator and passes the Q# code to it. The simulator uses the Q# code to create qubits (simulations of quantum particles) and apply transformations to modify their state. The results of the quantum operations in the simulator are then returned to the program. 18 | 19 | Isolating the Q# code in the simulator ensures that the algorithms follow the laws of quantum physics and can run correctly on quantum computers. 20 | 21 | ![qsharp-code-flow](https://user-images.githubusercontent.com/13979489/121571121-4a118a00-ca2b-11eb-96e0-a0712902ff85.png) 22 | 23 | 24 | Qubits as opaque references 25 | --- 26 | In Q# qubits are modeled as opaque data types that represent a reference to a specific two-state quantum system, whether physical or logical (error-corrected), on which quantum operations such as Hadamard or Pauli X may be performed. This is an operational view of qubits: qubits are defined by what you can do to them. 27 | 28 | The representation used in Q# has the interesting implication that all of the actual quantum computing is done by side effect. There is no way to directly interact with the quantum state of the computer; it has no software representation at all. Instead, one performs operations on qubit entities that have the side effect of modifying the quantum state. Effectively, the quantum state of the computer is an opaque global variable that is inaccessible except through a small set of accessor primitives (measurements) — and even these accessors have side effects on the quantum state, and so are really “mutators with results” rather than true accessors. 29 | 30 | Therefore, Q# has no ability to introspect into the state of a qubit or other properties of quantum mechanics directly, which guarantees that a Q# program can be physically executed on any quantum computer. Instead, a Q# program can call operations such as Measure to extract classical information from a qubit. 31 | 32 | Programing quantum operations 33 | --- 34 | Once allocated, a qubit can be passed to operations and functions, also referred to as callables. In some sense, this is all that a Q# program can do with a qubit. Any direct actions on state of a qubit are all defined by intrinsic callables such as X and H - that is, callables whose implementations are not defined within Q# but are instead defined by the target machine. What these operations actually do is only made concrete by the target machine you choose to run the particular Q# program. For more information, see Q# program implementation. 35 | 36 | For example 37 | --- 38 | if running the program on the full-state simulator, the simulator performs the corresponding mathematical operations to the simulated quantum system. But looking toward the future, when the target machine is a real quantum computer, calling such operations in Q# will direct the quantum computer to perform the corresponding real operations on the real quantum hardware. For example, in a trapped-ion quantum computer the quantum operations are realized by precisely timed laser pulses. 39 | 40 | A Q# program 41 | --- 42 | recombines these operations as defined by a target machine to create new, higher-level operations to express quantum computation. In this way, Q# makes it easy to express the logic underlying quantum and hybrid quantum–classical algorithms, while also being general with respect to the structure of a target machine or simulator. 43 | 44 | A simple example is the following program, which allocates one qubit in the 45 | |0⟩ 46 | state, then applies a Hadamard operation H to it and measures the result in the PauliZ basis. 47 | 48 | 49 | 50 | 51 | 52 | # Optimization Solver: A Qiskit Function by Q-CTRL Fire Opal 53 | 54 | ### Overview 55 | 56 | With the Fire Opal Optimization Solver, you can solve utility-scale optimization problems on quantum hardware without requiring quantum expertise. Simply input the high-level problem definition, and the Solver takes care of the rest. The entire workflow is noise-aware and leverages Fire Opal's Performance Management under the hood. The Solver consistently delivers accurate solutions to classically challenging problems, even at full-device scale on the largest IBM® QPUs. 57 | 58 | The Solver is flexible and can be used to solve combinatorial optimization problems defined as objective functions or arbitrary graphs. Problems do not have to be mapped to device topology. Both unconstrained and constrained problems are solvable, given that constraints can be formulated as penalty terms. The examples included in this guide demonstrate how to solve an unconstrained and a constrained utility-scale optimization problem using different Solver input types. The first example involves a Max-Cut problem defined on a 156-node, 3-Regular graph, while the second example tackles a 50-node Minimum Vertex Cover problem defined by a cost function. 59 | 60 | ### Function description 61 | 62 | The Solver fully optimizes and automates the entire algorithm, from error suppression at the hardware level to efficient problem mapping and closed-loop classical optimization. Behind the scenes, the Solver's pipeline reduces errors at every stage, enabling the enhanced performance required to meaningfully scale. The underlying workflow is inspired by the Quantum Approximate Optimization Algorithm (QAOA), which is a hybrid quantum-classical algorithm. For a detailed summary of the full Optimization Solver workflow, refer to the published manuscript. 63 | 64 | ![image](https://github.com/user-attachments/assets/1da79252-6e3a-4b1c-9b02-e63cdafc75d8) 65 | 66 | 67 | ### To solve a generic problem with the Optimization Solver: 68 | 69 | - Define your problem as an objective function or a graph. 70 | - Connect to the function through the Qiskit Functions Catalog. 71 | - Run the problem with the Solver and retrieve results. 72 | 73 | 74 | ### Get started 75 | 76 | ```qs 77 | from qiskit_ibm_catalog import QiskitFunctionsCatalog 78 | 79 | # If you have not previously saved your credentials, follow instructions at 80 | # https://docs.quantum.ibm.com/guides/setup-channel#iqp 81 | # to authenticate with your API token. 82 | catalog = QiskitFunctionsCatalog() 83 | 84 | # Access Function 85 | solver = catalog.load("q-ctrl/optimization-solver") 86 | ``` 87 | 88 | ### Example: Unconstrained optimization 89 | 90 | Run the maximum cut (Max-Cut) problem. The following example demonstrates the Solver's capabilities on a 156-node, 3-regular unweighted graph Max-Cut problem, but you can also solve weighted graph problems. 91 | 92 | In addition to qiskit-ibm-catalog, you will also use the following packages to run this example: networkx and numpy. You can install these packages by uncommenting the following cell if you are running this example in a notebook using the IPython kernel. 93 | 94 | ``` 95 | # %pip install networkx numpy 96 | ``` 97 | 98 | #### Define the problem 99 | ```qs 100 | import networkx as nx 101 | import numpy as np 102 | 103 | # Generate a random graph with 156 nodes 104 | maxcut_graph = nx.random_regular_graph(d=3, n=156, seed=8) 105 | ``` 106 | ```qs 107 | # Optionally, visualize the graph 108 | nx.draw(maxcut_graph, nx.kamada_kawai_layout(maxcut_graph), node_size=100) 109 | ``` 110 | 111 | ![image](https://github.com/user-attachments/assets/53c7383a-bbbe-433c-a57d-0b9e6c4ef5c2) 112 | 113 | The Solver accepts a string as the problem definition input. 114 | 115 | ```qs 116 | # Convert graph to string 117 | problem_as_str = nx.readwrite.json_graph.adjacency_data(maxcut_graph) 118 | ``` 119 | 120 | #### Run the problem 121 | 122 | ```qs 123 | # Solve the problem 124 | maxcut_job = solver.run( 125 | problem=problem_as_str, 126 | problem_type="maxcut", 127 | instance=instance, # E.g. "ibm-q/open/main" 128 | backend_name=backend_name, # E.g. "ibm_kyiv" 129 | ) 130 | ``` 131 | ```qs 132 | Check your Qiskit Function workload's status or return results as follows: 133 | ``` 134 | ```qs 135 | # Get job status 136 | print(maxcut_job.status()) 137 | ``` 138 | 139 | 140 | -------------------------------------------------------------------------------- /sample.qs: -------------------------------------------------------------------------------- 1 | //Tutorial: Implement a Quantum Random Number Generator in Q# 2 | //A simple example of a quantum algorithm written in Q# is a quantum random number generator. 3 | //This algorithm leverages the nature of quantum mechanics to produce a random number. 4 | 5 | namespace Qrng { 6 | open Microsoft.Quantum.Convert; 7 | open Microsoft.Quantum.Math; 8 | open Microsoft.Quantum.Measurement; 9 | open Microsoft.Quantum.Canon; 10 | open Microsoft.Quantum.Intrinsic; 11 | @EntryPoint() 12 | operation SampleQuantumRandomNumberGenerator() : Result { 13 | use q = Qubit(); // Allocate a qubit. 14 | H(q); // Put the qubit to superposition. It now has a 50% chance of being 0 or 1. 15 | return MResetZ(q); // Measure the qubit value. 16 | } 17 | } 18 | 19 | //Creating a complete random number generator 20 | //Now that we have a Q# operation that generates random bits, 21 | //we can use it to build a complete quantum random number generator. 22 | //We can use a Q# application or use a host program. 23 | 24 | 25 | 26 | namespace Qrng { 27 | open Microsoft.Quantum.Convert; 28 | open Microsoft.Quantum.Math; 29 | open Microsoft.Quantum.Measurement; 30 | open Microsoft.Quantum.Canon; 31 | open Microsoft.Quantum.Intrinsic; 32 | operation SampleQuantumRandomNumberGenerator() : Result { 33 | use q = Qubit(); // Allocate a qubit. 34 | H(q); // Put the qubit to superposition. It now has a 50% chance of being 0 or 1. 35 | return MResetZ(q); // Measure the qubit value. 36 | } 37 | 38 | operation SampleRandomNumberInRange(max : Int) : Int { 39 | mutable bits = new Result[0]; 40 | for idxBit in 1..BitSizeI(max) { 41 | set bits += [SampleQuantumRandomNumberGenerator()]; 42 | } 43 | let sample = ResultArrayAsInt(bits); 44 | return sample > max 45 | ? SampleRandomNumberInRange(max) 46 | | sample; 47 | } 48 | 49 | @EntryPoint() 50 | operation SampleRandomNumber() : Int { 51 | let max = 50; 52 | 53 | Message($"Sampling a random number between 0 and {max}: "); 54 | return SampleRandomNumberInRange(max); 55 | } 56 | } 57 | --------------------------------------------------------------------------------