Dynex Circuit Examples - dynexcoin/DynexSDK GitHub Wiki
Simple Circuit Examples
The quantum circuit performs a sequence of operations on two qubits:
- RX Gate: The first qubit is rotated around the X-axis by an angle of π radians, effectively flipping its state.
- RY Gate: The second qubit is rotated around the Y-axis by π radians, also flipping its state.
- CNOT Gate: A controlled-NOT gate entangles the two qubits, flipping the second qubit only if the first qubit is in the |1β© state.
- Hadamard Gate: The first qubit is then placed into a superposition of states.
The circuit concludes by returning the full quantum state of the system, which will be an entangled state reflecting the operations applied.
Medium Sized Circuit Example
This quantum circuit involves three qubits and performs a sequence of operations designed to create an entangled state and manipulate the qubits in various ways:
- Hadamard Gate: The first qubit is placed into a superposition.
- CNOT Gate: Entangles the first and second qubits.
- RX Gate: Rotates the second qubit around the X-axis by 0.3 radians.
- CRZ Gate: Applies a controlled-Z rotation between the first and second qubits by 0.1 radians.
- T Gate: Applies a Ο/8 phase shift to the second qubit.
- Toffoli Gate: A controlled-controlled-NOT gate that flips the third qubit if both the first and second qubits are in the |1β© state.
- SWAP Gate: Swaps the states of the second and third qubits.
The circuit concludes by returning the final quantum state, reflecting the complex interactions and entanglement between the qubits.
Complex Circuit Example (1)
This quantum circuit operates on four qubits and applies a series of complex operations to create an entangled and manipulated quantum state:
- Hadamard Gates: Each qubit is placed in a superposition state.
- CNOT Gates: These gates entangle the qubits sequentially from qubit 0 to qubit 3.
- Single-Qubit Rotations: RX, RY, and RZ gates rotate the qubits by specific angles given in params.
- Controlled-Z Gate: Introduces phase entanglement between qubits 0 and 2.
- Controlled-RZ Gate: Adds a phase rotation to qubit 3, controlled by qubit 1.
- Toffoli Gate: A controlled-controlled-NOT that flips qubit 3 based on the states of qubits 0 and 2.
- SWAP Gates: Swap the states of qubits 0 with 3 and 1 with 2.
- Additional Controlled-RZ and CRY Gates: These gates introduce further controlled rotations between qubits.
The circuit concludes by returning the final quantum state, which is highly entangled and complex due to the sequence of operations applied.
Complex Circuit Example (2)
This complex quantum circuit operates on 12 qubits, executing a series of operations designed to create intricate entanglements and rotations:
- Hadamard Gates: Each qubit is placed in superposition.
- CNOT Gates: Pairs of qubits are entangled across the register.
- Single-Qubit Rotations: RX, RY, and RZ gates are applied to each qubit according to the specified parameters.
- Controlled Operations: CZ and CRZ gates add phase entanglements, while Toffoli gates introduce more complex three-qubit interactions.
- SWAP Gates: Several pairs of qubits are swapped to redistribute their states.
The circuit culminates in a highly entangled quantum state with complex interdependencies among the qubits, returned by qml.state()
.
13 Qubit Full Adder Example
This quantum circuit implements a 13-bit full adder using quantum gates. The circuit operates as follows:
- BasisEmbedding: The integer a is encoded into the quantum states of the qubits.
- Quantum Fourier Transform (QFT): The QFT is applied to transform the quantum state into the Fourier basis, preparing the qubits for the addition operation.
- Kfourier Function: This function applies a phase shift to each qubit based on the integer b, effectively adding
b
toa
in the Fourier domain. - Inverse QFT (Adjoint QFT): The inverse QFT is applied to bring the qubits back to the computational basis, where the result of the addition is now encoded.
The circuit can return either the full quantum state or a sampled measurement result, depending on the state
parameter. This implementation efficiently adds two large binary numbers using the principles of quantum computing, leveraging the Fourier transform for phase encoding and addition.
Simple Grover Integer Factorisation Circuit
This quantum circuit implements Grover's algorithm for integer factorization using quantum gates. The circuit involves several qubit registers: wires_p
and wires_q
represent the prime factors, while wires_solution
stores the product. Here's a breakdown of the operations:
- Hadamard Gates: Apply superposition to all qubits in
wires_p
andwires_q
. - Multiplication Function: This function uses the Quantum Fourier Transform (QFT) and controlled rotations (Kfourier) to multiply the values in
wires_p
andwires_q
, storing the result inwires_solution
. - FlipSign and Grover's Operator: These operations perform the amplitude amplification, essential for Grover's search algorithm, to iteratively enhance the probability of measuring the correct factors.
The circuit returns the probabilities of each possible combination of factors after the Grover iterations. This process leverages the power of quantum parallelism and Grover's algorithm to factorize the integer n
.
Simple Shor Integer Factorisation Circuit
This Shorβs algorithm circuit is designed to factorize an integer π using quantum computation, particularly by finding the period (or order) of a modular function. Here's how it works:
-
Quantum State Preparation: The circuit prepares a superposition of states using Hadamard gates on the qubits responsible for estimating the period (estimate qubits). The last target qubit is initialized in the |1β© state using a Pauli-X gate.
-
Controlled Unitaries: Controlled operations apply powers of a unitary matrix πππ΄ (constructed from the integers π and π) to the target qubits, controlled by the estimate qubits. These operations encode the modular exponentiation function into the quantum state.
-
Inverse Quantum Fourier Transform (QFT): The inverse QFT is applied to the estimate qubits to extract the phase information, which is related to the period of the function.
-
Measurement: The circuit samples the final state, which provides information about the period.
-
Post-Processing: The sampled result is used to calculate the period, from which the factors of π are derived.
Shorβs algorithm leverages quantum parallelism and the QFT to efficiently factorize large integers, a problem that is classically hard but feasible with quantum computing. This circuit is a practical implementation of Shorβs algorithm, targeting specific numbers like π=35 with a randomly chosen base π=12. The algorithm iteratively searches for the period π, and once found, uses it to derive the prime factors of π.
Quantum Self-Attention Transformer
The Quantum Self-Attention Transformer leverages the principles of quantum computing to perform tasks typically handled by classical transformers, particularly in the domain of NLP and LLM. Classical transformers rely heavily on the self-attention mechanism, which allows the model to weigh the importance of different words in a sentence when making predictions or generating new text.
The quantum self-attention transformer circuit is designed to process word embeddings derived from sentences and generate new sentences based on quantum operations. The circuit begins by embedding the binary representation of input vectors into quantum states, followed by multiple layers of rotation and controlled gates to capture complex relationships between the inputs. After applying the Quantum Fourier Transform (QFT) and Grover's operator, the circuit uses a combination of Hadamard, T, and rotation gates to further process the information. The final output is a set of expectation values, which are processed with softmax to generate attention-weighted outputs. These outputs are then used to generate a new sentence by combining the embeddings with word vectors similar to the quantum-generated outputs, ensuring a coherent and contextually relevant sentence.
-
Tokenization and Embedding: The sentences are tokenized into individual words and converted to lower case. Each word is then converted into an 8-dimensional vector using the Word2Vec model.
-
Quantum Circuit Initialization: The circuit initializes 8 qubits, corresponding to the 8 dimensions of the word embeddings. The binary representation of each embedding vector (depending on whether each component is positive or negative) is encoded into the quantum state using qml.BasisEmbedding.
-
Quantum Self-Attention Layers: Rotation Layers: For three layers, RX, RY, and RZ rotations are applied to each qubit based on the input embedding values. This step manipulates the quantum state to capture complex relationships between different features of the word embeddings. Entanglement: Controlled rotations (CRZ) and CNOT gates are applied between pairs of qubits, further entangling them and allowing for the quantum circuit to process interactions between different components of the input vectors.
-
Fourier Transform and Grover's Operator: The Quantum Fourier Transform (QFT) is applied, followed by its adjoint to shift the quantum states into a frequency domain and back. Grover's Operator is used to amplify the correct quantum states, which helps in focusing on the most relevant features or aspects of the inputs.
-
Final Quantum Operations: A combination of Hadamard gates, T gates, and RZ rotations is applied to each qubit to further manipulate the quantum state, making it ready for measurement.
-
Measurement and Attention Weights: The circuit outputs the expectation values of each qubit in the Z-basis. These values are processed through a softmax function to obtain attention weights, which indicate the importance of each word in the context of the sentence.
-
Sentence Generation: The attention-weighted outputs are combined with the original word embeddings to generate a new sentence. The algorithm selects words with the highest similarity to the quantum-processed embeddings, resulting in a contextually relevant sentence generated based on the original input sentence.
This circuit essentially uses quantum computation to perform the role of attention in a transformer model, which is crucial for tasks like natural language processing, where understanding the importance of different words in a sentence is key to generating meaningful text. The quantum approach aims to leverage the potential speedups and parallelism inherent in quantum computing to perform these tasks more efficiently.