Quantum attention mechanism method based on quantum circuit

By evolving the quantum state of classical sequence data in quantum circuits and outputting similarity and density matrices, it solves the problem of increased computing resources of the attention mechanism in deep learning and achieves an efficient computing process.

CN116187454BActive Publication Date: 2024-09-20TURINGQ CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210840593.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-07-18
Publication Date
2024-09-20
Estimated Expiration
2042-07-18

AI Technical Summary

Technical Problem

As the input sequence data length increases, the classical computing resources required by the attention mechanism common in deep learning neural networks increase, resulting in a decrease in computational efficiency.

Method used

Using a quantum attention mechanism based on quantum circuits, by encoding classical sequence data into quantum states and evolving them in the quantum circuit, the similarity matrix and density matrix are output, eliminating the softmax normalization operation in the classical attention mechanism and saving money. Computing resources.

Benefits of technology

It achieves the use of less classical computing resources, improves computing efficiency, and significantly reduces computing overhead through the expressive ability and normalization characteristics of quantum circuits.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116187454B
    Figure CN116187454B
Patent Text Reader

Abstract

The present invention provides a quantum attention mechanism method based on quantum circuits, which belongs to the field of quantum computing. According to a quantum attention mechanism method based on quantum circuits provided by the present invention, the method encodes classical sequence data into a first quantum state, and inputs the first quantum state into three quantum circuits respectively, performs three unitary transformations and outputs three corresponding processing matrices Q, K, and V, evolves the processing matrices Q and K in a similarity circuit, and outputs the similarity α of the processing matrices Q and K. ij , the similarity α ij After linear transformation, the third processing matrix V is used as the parameter of the quantum attention circuit, and the third quantum state V is output after evolution. i ', and the third quantum state V i The input is evolved in the merged quantum circuit, and the density matrix corresponding to the classical sequence data is output after measurement.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of quantum computing, and in particular relates to a quantum attention mechanism method based on quantum circuits. Background Art

[0002] Attention mechanisms can help the model assign different weights to each part of the input, extract more critical and important information, and make the model make more accurate judgments without adding more overhead to the model's calculation and storage. The attention mechanism is simple, yet can give the model stronger discrimination capabilities. It can also be used to explain neural network models (e.g., alignment of input and output text in machine translation, and the degree of correlation between text and different areas of an image in image interpretation).

[0003] At present, deep learning neural network structures generally include attention mechanisms, such as the attention mechanism in the Seq2Seq model or the multi-head attention mechanism in the Transformer model.

[0004] However, as the length of the input sequence data increases, the model becomes more complex and therefore requires more classical computing resources. Summary of the invention

[0005] The present invention aims to provide a quantum attention mechanism method based on quantum circuits, thereby using fewer classical computing resources and improving computing efficiency.

[0006] The present invention provides a quantum attention mechanism method based on quantum circuits, comprising the following steps:

[0007] Acquire classical sequence data and encode it into a first quantum state containing classical sequence information;

[0008] The first quantum state is respectively input into the first quantum circuit, the second quantum circuit and the third quantum circuit for evolution, and the corresponding first processing matrix Q, the second processing matrix K and the third processing matrix V are output, wherein the first processing matrix Q includes a plurality of first vectors Q i The second processing matrix K includes a plurality of second vectors K j , the third processing matrix V includes a plurality of third vectors V i ;

[0009] The first vector Q in the first processing matrix Q 1 With multiple second vectors K j Input into the similarity metric subcircuit for evolution, and output the first vector Q after measurement 1 The corresponding similarity α 1j ;

[0010] Traverse multiple first vectors Q i , output the similarity α between the first processing matrix Q and the second processing matrix K ij ;

[0011] For similarity α ij Perform linear transformation and output the transformed similarity α ij ' and use it as the parameter of the quantum gate in the quantum attention circuit;

[0012] Input multiple third vectors Vi into the quantum attention circuit for evolution, and output the corresponding third quantum state V after measurement. i ';as well as

[0013] The third quantum state V i ′Input into the merged quantum circuit for evolution, and output the density matrix corresponding to the classical sequence data after measurement.

[0014] Wherein, i=1, 2…m, j=1, 2…m, m is a positive integer and m is the total length of the classical sequence data.

[0015] The above method, wherein:

[0016] The first quantum circuit, the second quantum circuit and the third quantum circuit include a variational quantum circuit module.

[0017] The above method, wherein:

[0018] The variational quantum circuit module includes an entanglement module and a single-bit rotation module.

[0019] The entanglement module includes N entanglement layers, which are used to entangle any two quantum bits.

[0020] The single-bit rotation module includes a single-bit rotation door that rotates along the Y axis, the Z axis and / or the X axis.

[0021] The above method, wherein:

[0022] Similarity measurement quantum circuits include Swap test quantum circuits.

[0023] The above method, wherein:

[0024] The swap test quantum circuit includes an H gate and a controlled SWAP gate.

[0025] The above method, wherein:

[0026] The merged quantum circuit includes auxiliary quantum circuit, target quantum circuit and quantum attention circuit.

[0027] The auxiliary quantum circuit acts as a control circuit to control the target quantum circuit and the quantum attention circuit to execute k controlled SAWP gates in sequence, and outputs the density matrix corresponding to the classical sequence data after measuring the target quantum circuit.

[0028] The above method, wherein:

[0029] The control bit of the auxiliary quantum circuit is The operations performed by the ath (a=1, 2, 3, ..., k) controlled SWAP gate include: In |a-1>, then for |z i > and |V a >Perform SWAP operation,

[0030] Among them, the number of quantum bits of the auxiliary quantum circuit is n a , |z i > is the quantum state of the target quantum circuit, |V a > is the quantum state of the quantum attention circuit.

[0031] The above method, wherein:

[0032] After executing k controlled SWAP gates in sequence, the quantum state of the merged quantum circuit |Φ total > is equal to |z i >、|V k >The tensor product between .

[0033] The above method, wherein:

[0034] It also includes deviation operations on quantum states in auxiliary quantum circuits and quantum attention circuits.

[0035] The above method, wherein:

[0036] Classic sequence data includes word vector sequence data or text vector sequence data.

[0037] Functions and Effects of the Invention

[0038] According to a quantum attention mechanism method based on quantum circuits provided by the present invention, the method encodes classical sequence data into a first quantum state, and inputs the first quantum state into three quantum circuits respectively, performs three unitary transformations and outputs three corresponding processing matrices Q, K, and V, evolves the processing matrices Q and K in the similarity circuit, and outputs the similarity α of the processing matrices Q and K. ij , the similarity α ij After linear transformation, the third processing matrix V is used as the parameter of the quantum attention circuit, and the third quantum state V is output after evolution. i', and the third quantum state V i 'Input the merged quantum circuit for evolution, and output the density matrix corresponding to the classical sequence data after measurement. Because the present invention can realize the attention information corresponding to the classical sequence data by constructing the corresponding quantum circuit, compared with the classical attention mechanism, because the quantum circuit has strong expression ability, a large amount of classical computing resources can be saved in the calculation process. At the same time, because the quantum state in the quantum circuit itself has normalization characteristics, the probability normalization operation using softmax in the classical attention mechanism can be omitted. Therefore, the quantum attention mechanism based on quantum circuits provided by the present invention can save a large amount of classical computing resources and improve computing efficiency compared with the classical attention mechanism. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 It is a general flow chart of the quantum attention mechanism method based on quantum circuits in an embodiment of the present invention.

[0040] Figure 2 It is a schematic diagram of the structure corresponding to the universal quantum circuit in an embodiment of the present invention.

[0041] Figure 3 It is a schematic diagram of the structure of the first entanglement layer of an embodiment of the present invention.

[0042] Figure 4 It is a schematic diagram of the structure of the second entanglement layer of an embodiment of the present invention.

[0043] Figure 5 It is a schematic diagram of the structure of the third entanglement layer of an embodiment of the present invention.

[0044] Figure 6 Schematic diagram of the structure of the similarity metric subcircuit in an embodiment of the present invention.

[0045] Figure 7 It is a schematic diagram of the structure of the merged quantum circuit according to an embodiment of the present invention. DETAILED DESCRIPTION

[0046] In order to make the technical means, creative features, objectives and effects achieved by the present invention easy to understand, the quantum attention mechanism method based on quantum circuits provided by the present invention is specifically described below in combination with embodiments and drawings.

[0047] <Example>

[0048] Figure 1 It is a general flow chart of the quantum attention mechanism method based on quantum circuits in an embodiment of the present invention.

[0049] like Figure 1 As shown, the quantum attention mechanism method based on quantum circuit provided by the embodiment of the present invention includes the following steps:

[0050] S1, obtaining classical sequence data and encoding it into a first quantum state containing classical sequence information;

[0051] S2, input the first quantum state into the first quantum circuit, the second quantum circuit and the third quantum circuit respectively for evolution, and output the corresponding first processing matrix Q, the second processing matrix K and the third processing matrix V, wherein the first processing matrix Q includes a plurality of first vectors Q i The second processing matrix K includes a plurality of second vectors K j , the third processing matrix V includes a plurality of third vectors V i ;

[0052] S3, the first vector Q in the first processing matrix Q 1 With multiple second vectors K j Input into the similarity metric subcircuit for evolution, and output the first vector Q after measurement 1 The corresponding similarity α 1j ;

[0053] S4. Traverse multiple first vectors Q i , output the similarity α between the first processing matrix Q and the second processing matrix K ij ;

[0054] S5. Similarity α ij Perform linear transformation and output the transformed similarity α ij ' and use it as the parameter of the quantum gate in the quantum attention circuit;

[0055] S6. Multiple third vectors V i The input is evolved in the quantum attention circuit, and the corresponding third quantum state V is output after measurement. i ';as well as

[0056] S7, the third quantum state V i ′ is input into the merged quantum circuit for evolution, and the density matrix corresponding to the classical sequence data is output after measurement.

[0057] Wherein, i=1, 2…m, j=1, 2…m, m is a positive integer and m is the total length of the classical sequence data.

[0058] The classic sequence data in this embodiment includes word vector sequence data or text vector sequence data.

[0059] Figure 2 It is a schematic diagram of the structure corresponding to the universal quantum circuit in an embodiment of the present invention.

[0060] This embodiment Figure 2 The universal quantum circuits provided can all be used to represent the first quantum circuit U query(θ), the second quantum circuit U Key (θ), the third quantum circuit U value (θ), similarity quantum circuit, quantum attention circuit U q,attn (θ′) and the quantum circuit structure of the merged quantum circuit.

[0061] Figure 2 The universal quantum circuit in includes the coding quantum circuit module U(x) and the variational quantum circuit module V(θ), where x in U(x) is the classical input data. By initializing the quantum bits in U(x), the initial quantum state corresponding to the entire classical input data is obtained. (n is the number of qubits). Apply U(x) to For example, the classical word vector sequence data is input into the encoding quantum circuit module U(x) for encoding, and the first quantum state containing the word vector sequence information is obtained. The first quantum state is then evolved in the variational quantum circuit module V(θ), and the density matrix corresponding to the word vector sequence is output after multiple measurements.

[0062] Figure 2 The variational quantum circuit module V(θ) in the example includes an entanglement module and a single-bit rotation module. The entanglement module includes N entanglement layers L i (θ), used to entangle any two quantum bits, the single-bit rotation module includes a series of single-bit rotation gates rotating along the Y axis, Z axis and / or X axis. Figure 2 The entanglement layer L in the entanglement module shown i The number of layers N of (θ) can be adjusted as needed, and each entanglement layer L i (θ) acts between any two different qubits. For example, a two-bit gate (such as a CNOT gate) can be applied between any two qubits to entangle the two qubits. Each entanglement layer L i (θ) may include a single-bit rotation submodule and a dual-bit operation submodule. The single-bit rotation submodule in this embodiment has adjustable parameters, and the dual-bit operation submodule has a fixed structure and does not have adjustable parameters.

[0063] Figure 3 is a schematic diagram of the structure of the first entanglement layer in an embodiment of the present invention; Figure 4 is a schematic diagram of the structure of a second entanglement layer in an embodiment of the present invention; Figure 5 It is a schematic diagram of the structure of the third entanglement layer of an embodiment of the present invention.

[0064] like Figure 3-5 As shown, the entanglement layer of the embodiment of the present invention may include three entanglement layer structures. Figure 3In the first entanglement layer, the single-bit rotation submodule includes a plurality of rotation combination gates R rotating along the Y axis, the Z axis, and the Y axis. y R z R y , each revolving door has an adjustable rotation angle, such as α i , β i , γ i , i is 1, 2...n, n is a positive integer and is the total number of quantum bits in the quantum circuit. The rotation combination gate used in this embodiment is R y R z R y In other embodiments, other rotating combination doors may be used to obtain any single-bit rotating door. Figure 4 , Figure 5 The single-bit rotation submodule in Figure 4 The revolving door can be combined or set to Figure 4 The same single-bit rotation submodule is not described again here.

[0065] Figure 3 The dual-bit operation submodule in may include a series of nearest neighbor CNOT gates (controlled NOT gates), for example, a CNOT gate is applied between any two adjacent quantum bits, the quantum bit where the black dot is located is used as the control bit, and the next quantum bit connected to it is entangled as the controlled bit, traversing all quantum bits so that the last CNOT gate is applied between the last quantum bit and the first quantum bit.

[0066] Figure 4 The dual-bit operation submodule in may include a series of CNOT gates (controlled NOT gates) of next-nearest neighbors. For example, a quantum bit is spaced between any two quantum bits, and a CNOT gate is applied between two quantum bits with an intermediate interval. The front quantum bit (the quantum bit where the black dot is located) serves as a control bit, and the next quantum bit connected to it serves as a controlled bit for entanglement, so that the last CNOT gate is applied between the last quantum bit and the second quantum bit.

[0067] Figure 5 The two-bit operation submodule in may include a series of CNOT gates (controlled NOT gates) of the nearest neighbor and the next nearest neighbor combination, that is, Figure 5 The series of CNOT gates in can be applied between two adjacent qubits or between two qubits separated by a qubit. Figure 3 , Figure 4 The description in will not be repeated here.

[0068] This embodiment only exemplarily shows three entangled layers with different structures. In other embodiments, entangled layers based on the same functions and concepts as those of this embodiment also fall within the scope of protection of this embodiment.

[0069] like Figure 2 The single-bit rotation module in the embodiment is composed of N single-bit arbitrary rotation gates. For example, the arbitrary rotation gate can be obtained by combining the rotation operations along the Y axis, Z axis, and Y axis, and each rotation gate has an adjustable rotation angle (α i , β i , γ i ). For example, the rotation angle of the single-bit rotation module in the quantum attention circuit provided in this embodiment is α ij ′, where i and j represent two different quantum states respectively.

[0070] Figure 6 Schematic diagram of the structure of the similarity metric subcircuit in an embodiment of the present invention.

[0071] like Figure 6 As shown, the similarity metric subcircuit in the embodiment of the present invention is used to calculate the similarity α between the first processing matrix Q and the second processing matrix K ij Specifically, the similarity metric subcircuit may include but is not limited to Figure 6 The Swap test quantum circuit shown in . Figure 6 The Swap test quantum circuit in includes two H gates and a SWAP controlled gate. Both H gates act on the auxiliary quantum bit, and the control bit of the SWAP controlled gate is the control bit in the auxiliary quantum circuit ( Figure 6 The black dot in the middle), the SWAP gate connected to it acts on two different quantum bits U φ (x), U φ (x′), so that the quantum states of the two qubits are exchanged. The quantum states of the swap test quantum circuit are measured in the Pauli Z direction, and the corresponding similarity α is output. ij Specifically, the first vectors Q in the first processing matrix Q are i Encoded as quantum state, multiple second vectors K j Encoded as quantum states, the two types of quantum states are initialized, and then evolved by applying SWAP controlled gates. The similarity α between the first processing matrix Q and the second processing matrix V is measured by the auxiliary quantum circuit. ij , that is, α ij = <z>.

[0072] Figure 7 It is a schematic diagram of the structure of the merged quantum circuit according to an embodiment of the present invention.

[0073] The merged quantum circuit in this embodiment includes an auxiliary quantum circuit, a target quantum circuit and a quantum attention circuit. The auxiliary quantum circuit serves as a control circuit to control the target quantum circuit and the quantum attention circuit to execute k controlled SAWP gates in sequence, and outputs a density matrix corresponding to the word vector sequence data after measuring the target quantum circuit.

[0074] like Figure 7 As shown, the control bit of the auxiliary quantum circuit in this embodiment is The operations performed by the ath (a=1, 2, 3, ..., k) controlled SWAP gate include: In |a-1>, then for |z i > and |V a >Perform a SWAP operation, where the number of quantum bits in the auxiliary quantum circuit is n a , |z i > is the target quantum circuit ρ i The quantum state of |V a > is the quantum state of the quantum attention circuit. After executing k controlled SWAP gates in sequence, the quantum state of the merged quantum circuit |Φ total > is equal to |z i >、|V k >The tensor product between .

[0075] Figure 7 Control bits of auxiliary quantum circuits The number of target quantum circuits is 3, and the target quantum circuit ρ i The number of quantum bits is 5, and each quantum state in the quantum attention circuit |V k >The number of quantum bits in each quantum circuit in the combined quantum circuit is 5. In other embodiments, the number of quantum bits in each quantum circuit in the combined quantum circuit can be adjusted appropriately, and this embodiment is only illustrative.

[0076] In this embodiment, the auxiliary quantum circuit is used as the control circuit, and the target quantum circuit ρ i The quantum attention circuit performs a controlled SWAP gate operation. The quantum bits of the auxiliary quantum circuit serve as control bits. The quantum state in the target quantum circuit ρi |z i > is the controlled bit, and the quantum state |Vk> in the quantum attention circuit is the controlled bit. Initialize the target quantum circuit ρ i After the initial quantum state of |0> is merged, the quantum state of the quantum circuit |Φ total > is expressed as follows:

[0077]

[0078] Among them, the control bit in the auxiliary quantum circuit The expression is as follows:

[0079]

[0080] The control bit After substituting the expression (Equation 2) into Equation (1), the quantum state of the combined quantum circuit |Φ total > is expressed as follows:

[0081]

[0082] The quantum state |Φ in the above merged quantum circuit total >(Formula 3) executes k controlled SWAP gates in sequence, and the operations performed by the ath (a=1, 2, 3, ..., k) controlled SWAP gate include: If |a-1> is |z i > and |V a >Execute the SWAP operation, and after executing all controlled SWAP gates, measure the quantum state of the merged quantum circuit |Φ total >:

[0083]

[0084] After all SWAP gate operations are performed on the quantum state |Φ total >(Equation 4) is simplified, where the k |0> with coefficient 1 in Equation 4 are the target quantum circuit ρ i The quantum state of |z i >, the target quantum circuit ρ i The k quantum states |0> in are rewritten as mutually orthogonal quantum states |Ψ 1 >,|Ψ 2 >, ..., |Ψ k >, and perform a deviation operation on the quantum states in the auxiliary quantum circuit and the quantum attention circuit, and output the density matrix ρ corresponding to the word vector sequence after measurement i , which is expressed as follows:

[0085]

[0086] In formula (5), the density matrix ρ after merging quantum circuits is i The word vector i in contains not only its own information, but also the association information between word vector i and another word vector i, that is, each word vector i processed by the quantum attention method based on quantum circuits is a mixed state.

[0087] The above description and drawings provide typical embodiments of specific structures of specific embodiments, and the above invention provides existing preferred embodiments, but these contents are not intended to be limiting. For those skilled in the art, various changes and modifications will undoubtedly be obvious after reading the above description. Therefore, the attached claims should be regarded as covering all changes and modifications to the true intent and scope of the present invention. Any and all equivalent scopes and contents within the scope of the claims should be considered to still be within the intent and scope of the present invention.< / z>

Claims

1. A quantum attention mechanism method based on quantum circuits, characterized in that: The steps include: Acquire classical sequence data and encode it into a first quantum state containing classical sequence information; The first quantum state is respectively input into the first quantum circuit, the second quantum circuit and the third quantum circuit for evolution, and the corresponding first processing matrix Q, the second processing matrix K and the third processing matrix V are output, wherein the first processing matrix Q includes a plurality of first vectors Q i The second processing matrix K includes a plurality of second vectors K j , the third processing matrix V includes a plurality of third vectors V i ; The first processing matrix Q includes a plurality of first vectors Q i A vector Q1 in the above-mentioned plurality of second vectors K j Input into the similarity metric subcircuit for evolution, and after measurement, the similarity α corresponding to the output vector Q1 is output 1j ; Traverse the plurality of first vectors Q i , output the similarity α between the first processing matrix Q and the second processing matrix K ij ; For the similarity α ij Perform linear transformation and output the transformed similarity α ij ' and use it as the parameter of the quantum gate in the quantum attention circuit; The plurality of third vectors V i Input into the quantum attention circuit for evolution, and output the corresponding third quantum state V after measurement i ';as well as The third quantum state V i ′ is input into the merged quantum circuit for evolution, and after measurement, the density matrix corresponding to the classical sequence data is output, Wherein, i=1, 2...m, j=1, 2...m, m is a positive integer and m is the total length of the classical sequence data; The first quantum circuit, the second quantum circuit and the third quantum circuit include a variational quantum circuit module; The variational quantum circuit module includes an entanglement module and a single-bit rotation module, wherein the entanglement module includes N entanglement layers for entanglement between any two quantum bits, and the single-bit rotation module includes a single-bit rotation gate that rotates along the Y axis, the Z axis and / or the X axis; The merged quantum circuit includes an auxiliary quantum circuit, a target quantum circuit and the quantum attention circuit. The auxiliary quantum circuit serves as a control circuit to control the target quantum circuit and the quantum attention circuit to sequentially execute k controlled SWAP gates, and outputs a density matrix corresponding to the classical sequence data after measuring the target quantum circuit. The control bit of the auxiliary quantum circuit is The operations performed by the a-th controlled SWAP gate include: is in |a-1), then for |z i > and |V a >Perform SWAP operation, The number of quantum bits in the auxiliary quantum circuit is n a , |z i > is the quantum state of the target quantum circuit, |V a > is the quantum state of the quantum attention circuit; After executing the k controlled SWAP gates in sequence, the quantum state of the merged quantum circuit |Φ total > is equal to |z i >、|V k >The tensor product between .

2. The method according to claim 1, characterized in that: in, The similarity measurement quantum circuit includes a Swap test quantum circuit.

3. The method according to claim 2, characterized in that: The Swap test quantum circuit includes an H gate and a controlled SWAP gate.

4. The method according to claim 1, characterized in that: Also includes: Performing a deviation operation on the quantum states in the auxiliary quantum circuit and the quantum attention circuit.

5. The method according to claim 1, characterized in that: The classic sequence data includes word vector sequence data or text vector sequence data.