Efficient quantum algorithms for set operations

Analyzing the relations between Boolean functions has many applications in many fields, such as database systems, cryptography, and collision problems. This paper proposes four quantum algorithms that use amplitude amplification techniques to perform set operations, including Intersection, Difference, and Union, on two Boolean functions in \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\mathcal {O}(\sqrt{\mathcal {N}} )$$\end{document}O(N) time complexity. The proposed algorithms employ two quantum amplitude amplification techniques divided into two stages. The first stage uses the Younes et al. algorithm for quantum searching via entanglement and partial diffusion to prepare incomplete superpositions of the truth set of the first Boolean function. In the second stage, a modified version of Arima’s algorithm, along with an oracle that represent the second Boolean function, is employed to handle the set operations. The proposed algorithms have a higher probability of success in more general and comprehensive applications when compared with relevant techniques in literature.


Background Set operations on Boolean functions
A Boolean function f is a function whose variables (arguments) take the values 0 (False) or 1 (True), i.e., f can be represented as follows f : Y n → Y , such that Y = {0, 1} .The domain Y n of f is the set of 2 n binary vectors.
Let f F ⊆ Y n be the set of binary vectors where the Boolean function f evaluated to False and f T ⊆ Y n be the set of binary vectors where the Boolean function f evaluated to True, where f T ∪ f F = Y n .
Let f 1 and f 2 be two Boolean functions; we define the set operations over Boolean functions as follows: 1. Intersection: f 1 ∩ f 2 to be the set of binary vectors that evaluates both f 1 and f 2 to True at the same time, i. e.
2. Union: f 1 ∪ f 2 to be the set of binary vectors that evaluates either f 1 or f 2 to True, i.e. f 1 ∪ f 2 = f T 1 ∪ f T 2 .3. Difference: f 1 − f 2 to be the set of binary vectors that evaluates f 1 to True and evaluates f 2 to False, i. e.

Quantum computing
The quantum bit or qubit 37 is the elementary unit of the data in quantum computing.The qubit can be in a combination of |0� and | 1� in a linear superposition as shown in Eq. 1, where α , β are complex numbers representing the probabilistic amplitudes of |0� and |1� respectively, and the condition in Eq. 2 must be satisfied by the amplitudes 38 , and (1) where |α| 2 = α.α* , α * is the complex conjugate transpose of α .When a measurement is carried out, the super- position is collapsed to one of the states in a probabilistic way, i. e., the superposition is collapsed to |0� with probability |α| 2 and |1� with probability |β| 2 .
Entanglement is one of the quantum features in which the quantum state has to be described for the whole system 39 , and each object of the quantum system cannot be described independently.Another property of quantum computing is parallelism, where it takes a quantum computer a single step to operate on n inputs with a single gate.In contrast, the classical computer takes 2 n steps for the same input size.Parallelism performs multiple operations at a time and does not require additional hardware or wait for other processes to complete.Quantum gates are unitary operators 19 , supposing that a gate has n inputs, and then it can be represented as 2 n × 2 n unitary matrix assuming that state |0� = 1 0 and state|1� = 0 1 .Some of the quantum gates 40 which will be used in the paper are: The X gate is similar to the NOT gate in classical computers, where it maps |0� to |1� and |1� to |0� as shown in the following equation: The Hadamard gate has the following effect when applied on |0� and |1�: The Z gate that does not vary the state|0� but it converts |1� to − |1� as shown in the following Quantum circuits are a cascade of basic quantum gates to carry out a specific operation.

Quantum searching algorithms
This section reviews the four quantum algorithms that will be used in the proposed algorithm: Grover's algorithm for searching an unstructured database uses Grover's diffusion operator G (inversion about the mean) 36 .Younes et al. algorithm for searching an unstructured list using a quantum operator D p that performs the inversion about the mean only on a subspace of the system (Partial Diffusion Operator) 36 .Ventura's algorithm searches in an incomplete superposition when the initial amplitude distribution of the dataset is non-uniform, and Arima's algorithm improved Ventura's algorithm to increase the probability of results 21 .

Grover's algorithm
Grover presented a quantum algorithm to search an unstructured database of N items in O √ N .Grover's algorithm prepares a quantum register with n + 1 qubits in a uniform superposition of qubits where the first n qubits are initialized to the state |0� and an extra workspace qubit initialized to the state |1� 36 , Then, the following steps must be iterated approximately π times, where ⌊⌋ is the floor operation.Grover's algorithm applies Hadamard gates on the n + 1 qubits as follows: After that, it applies the oracle operator O G that provides the amplitudes of the matches phase shift of e iπ36 and evaluates a Boolean function f G : {0, 1} n → {0, 1} as shown in Eq. 11. (2) (5) The algorithm then applies the diffusion operator G on the first n qubits to make inversion about the mean, and G is shown in Eq. 12 : where I n is the identity matrix of size N × N .Measurement is then applied on the first n qubits to retrieve one of the searched items.Assume that ′ is the sum over the desired C matches, while ′′ represents the sum over the N − C undesired matches.The system after q G ≥ 1 iterations can be written as follows, where the amplitudes a g and b g after q G ≥ 1 iterations are defined by the following recurrence relations 15 .
Solving these recurrence relations, the closed forms can be written as follows 15 : where

Younes et al. algorithm
Younes et al. algorithm can search an unstructured database of N items with a higher probability of success via the partial diffusion operator This algorithm prepares a complete superposition and amplifies the solutions' amplitudes via entanglement of the search space with the extra qubit, which is useful in that it can be done by applying the measurement on the extra qubit 18,36 .This algorithm applies the oracle operator O Y on n + 1 qubits where O Y evaluates the Boolean function f Y : {0, 1} n → {0, 1} as shown in the following equation: Then, the algorithm applies the D P the operator, which can take the form as described in the following equation 36 : where |0� 's size is 2N = 2 n+1 , and the identity matrix I k is of size 2 k × 2 k .Suppose a general system |ψ Y 2 � of n + 1 qubits as follows: where α r = δ k : k even and β r = δ k : k odd.
Hence, applying D P on the general system has the following effect: where �α� = 1 √ N N −1 r=0 α r /N is the mean of the amplitudes of the subspace entangled with the |0� of the extra qubit.The O Y and D P operators are iterated q y times where q y is as follows: Assume that ′ is the sum over the desired C matches, while ′′ represents the sum over the N − C undesired matches.The system after q y > 1 iterations can be described as follows: (15) Vol marks any state in stored data m.Ventura's algorithm can be summarized as follows 22 .
Given: Phase oracles I f T 7. Observe the system.

Arima's algorithm
Arima search algorithm was proposed to solve the search in an incomplete superposition when the initial amplitude distribution of the dataset in non-uniform cases means that N does not equal the number of stored data and improves the venture search performance algorithm.Arima's algorithm can be summarized as follows 21 , Given: Phase oracles 6. Observe the system.

The proposed algorithms
In this section, we will propose algorithms supposing that f 1 and f 2 are two Boolean functions with n Boolean inputs and f T 1 and f T 2 are the set of binary vectors where the Boolean function f 1 and f 2 evaluated to True respectively.The proposed algorithms consist of two stages: the first stage prepares an incomplete superposition of a search space with specific properties using the Younes et al. algorithm 36 for searching a state that satisfies the oracle that represents the I f T 1 .The second stage prepares an incomplete superposition of a search space with specific properties using an updated version of Arima's algorithm 21,41 for searching a state in the oracle I f T 2 that satisfies with a state in the oracle I f T 1 .

The proposed quantum algorithm for true intersection operation
Given two Boolean functions f 1 and f 2 , it is required to find the set of binary vectors that evaluates both f 1 and f 2 to True simultaneously, i. e., to find the intersection between them, the steps of the proposed algorithm will be illustrated using this example: If n = 4 , the possible number of items to the Boolean function equals N , where N = 16 .Assume that the number of stored elements in f 1 and f 2 equals m, where m = 8 .Suppose that f 1 evalu- ates to true for each pattern in the set { |0�, |1�, |3�, |5�, |7�, |9�, |11�, |15�}, and f 2 evaluates to true for each pattern in the set { |0�, |2�, |4�, |6�, |8�, |10�, |12�, |15�}.

Apply Younes et al. algorithm 36 as follows:
(a) The preparation of the register consists of n + 1 qubits, and all of them are in the state |0� .The auxiliary qubit is used to evaluate the Boolean function f 1 .
When applying Eq. ( 22) to the illustrative example, the form will be as follows: www.nature.com/scientificreports/(b) The initialization of the register in which the Hadamard gate H is applied on the first n qubits in parallel as in Eq. ( 24).
When applying Eq. (24) to the illustrative example, the form will be as follows: (c) This algorithm iterates the following steps for π such that When applying Eq. (26) to the illustrative example, the form will be as follows: (ii) After that, the algorithm applies the partial diffusion D p on the n + 1 qubits.Assume that C be the number of elements common to f T 1 and f T 2 such that 1 ≤ C ≤ N .Let r ′ represents the intersected items and r ′′ represents undesired items in the truth set so |ψ 2 � can be rewritten as follows: After applying D p to |ψ 2 � the system can be described as follows: The mean of the amplitudes to the illustrative example is www.nature.com/scientificreports/then the inversions about the mean of the amplitudes 36 are as follows: Hence, applying the partial diffusion D p can take this form: (d) Apply the measurement on the auxiliary qubit, and if the outcome equals to one, we apply Z followed by H on the auxiliary qubit; otherwise, restart the previous steps.The probability to get |1 � on the auxiliary qubit is C|c q | 2 and the superposition can be represented as follows: Applying the measurement on the auxiliary qubit to the illustrative example is as follows: Applying Z on the auxiliary qubit to the illustrative example is as follows: Applying H on the auxiliary qubit to the illustrative example is as follows: to the illustrative example, the form will be as follows: www.nature.com/scientificreports/ The mean of the amplitudes to the illustrative example is then, the inversions about the mean of the amplitudes are as follows 21 : such that β represents the amplitude of any state in the stored data, but it is not the desired element, αrepresents the amplitude of the state of the desired element, and γ represents the amplitude of any state not in the stored data and not be the desired element.Hence, applying the Grover operator G can take this form: When applying I f T 1 to the illustrative example, the form will be as follows: = 1 16 for the amplitudes of states to the illustrative example which product to the state |0� .Then, the inversions of the mean for the amplitudes of this states are as follows: = −1 16 for the amplitudes of states to the illustrative example which product to the state |1� , then, the inversions of the mean for amplitudes of this states are as follows: Arima algorithm can solve the illustrative example by searching in f 1 , supposing that the searching data are |0� and |15� and P = 1 .In order to save space, instead of writing out the entire superposition of states, a transposed vector of coefficients will be used, where the 16 basis states index the vector.
Then the probability = 1 √ 2 2 = 0.50 for either state |0� or |15� as the same result as the proposed algorithm.A pseudocode of true intersection operation is shown in Algorithm 1; the circuit of the proposed algorithm is shown in Fig. 1. www.nature.com/scientificreports/Data: Given two Boolean functions f 1 and f 2 with n ≥ 0 inputs, N items and C is the number of matches.Result: Find the intersection I between f 1 and f 2 which makes them evaluate to true, i. e., find

The proposed quantum algorithm for false intersection operation
Given two Boolean functions f 1 and f 2 it is required to find the set of binary vectors that evaluates both f 1 ⊕ 1 42 and f 2 ⊕ 1 to True at the same time, i. e., to find the false intersection between them, the steps of the proposed algorithm will be illustrated using the illustrative example: 1. Apply Younes et al. algorithm 36 as follows: (a) The preparation of the register consists of n + 1 qubits, and all of them are in the state |0� .The auxiliary qubit is used to evaluate the Boolean function f 1 .The state of the system is as in Eqs. ( 22) and ( 23).(b) The initialization of the register in which the Hadamard gate H is applied on the first n qubits in parallel as in Eqs. ( 24) and ( 25).
(ii) Apply X on the auxiliary qubit 42 as follows in Eq. (53).
When applying Eq. (53) to the illustrative example, the form will be as follows: (iii) After that, the algorithm applies the partial diffusion D p on the n + 1 qubits as follows: (53) www.nature.com/scientificreports/ The mean of amplitudes to the illustrative example is then the inversions about the mean of the amplitudes are Hence, applying the partial diffusion D p can take this form (d) Apply the measurement on the auxiliary qubit, and if the outcome equals to one, we apply Z followed by H on the auxiliary qubit; otherwise, restart the previous steps.The probability to get |1 � on the auxiliary qubit is C|c q | 2 and the superposition can be represented as follows: Applying the measurement on the auxiliary qubit to the illustrative example is as follows: Applying Z on the auxiliary qubit to the illustrative example is as follows: Applying H on the auxiliary qubit to the illustrative example is as follows: When applying I f T 2 to the illustrative example, the form will be as follows: (55) (58) (61) Applying X on the auxiliary qubit to the illustrative example is as follows: The mean of the amplitudes to the illustrative example is then, the inversions about the mean of the amplitudes for states are as follows: Hence, applying the Grover operator G can take this form: When applying I f T 1 to the illustrative example, the form will be as follows: (63) (69) Vol = −1 16 for the amplitudes of states to the illustrative example which product to the state |0� .Then, the inversions of the mean for the amplitudes of these states are as follows: and �α� = = 1 16 for the amplitudes of states to the illustrative example which product to the state|1� .Then, the inversions of the mean for amplitudes of these states are as follows: Hence, applying the Grover operator G can take this form (e) Observe the system.
3. Find the probability of a match out of the R possible match between N items as in Eq. (52).
To the illustrative example, the intersection that makes f 1 and f 2 evaluate to False can be obtained with the probability 2 * 1 2 2 = 0.50 for |13� or |14�.
Then the probability = −1 √ 2 2 = 0.50 for any either |13� or|14� as the same result of the proposed algorithm.
A pseudocode of false intersection operation is shown in Algorithm 2; the circuit of the proposed algorithm is shown in Fig. 2. (70) Vol:.( 1234567890) www.nature.com/scientificreports/Data: Given two Boolean functions f 1 and f 2 with n ≥ 0 inputs, N items and C is the number of matches.Result: Find the intersection I between (f 1 ⊕ 1) and (f 2 ⊕ 1) which makes them evaluate to true, i. e., find

The proposed quantum algorithm for difference operation
Given two Boolean functions f 1 and f 2 , it is required to find the set of binary vectors that evaluates f 1 to True and evaluates f 2 to False, i. e., to find the difference between f 1 and f 2 , and the steps of the proposed algorithm will be illustrated using the illustrative example: 1. Apply Younes et al. algorithm as follows: (a) The preparation of the register consists of n + 1 qubits, and all of them are in the state |0� .The auxiliary qubit is used to evaluate the Boolean function f 1 .The state of the system is as in Eq. ( 22) and Eq. ( 23).(b) The initialization of the register in which the Hadamard H is applied on the first n qubits in parallel as in Eqs. ( 24) and ( 25).
(c) This algorithm iterates the following steps for π where I f T 1 evaluates the first boolean function f 1 as in equations ( 26), (27), and (28).After that, the algorithm applies the partial diffusion D p on the n + 1 qubits as follows in Eqs. ( 29) and (33): Quantum circuit for the proposed false intersection algorithm.
(d) Apply the measurement on the auxiliary qubit, and if the outcome equals to one, we apply Z followed by H on the auxiliary qubit; otherwise, restart the previous steps.The probability of getting |1 � on the auxiliary qubit is C|c q | 2 , and the superposition can be represented as in Eqs.(34-37).
2. Apply the Arima algorithm for P = (π √ 2N )/8 times to find difference as follows: (a) When applying I f T 2 to the illustrative example, the form will be as follows: Applying X on the auxiliary qubit to the illustrative example is as follows The mean of the amplitudes to the illustrative example is then, the inversions about the mean of the amplitudes of states are as follows: Hence, applying the Grover operator G can this form: When applying I f T 1 to the illustrative example, the form will be as follows: (78) = −1 16 for the amplitudes of states to the illustrative example which product to the state |0� .Then, the inversions about the mean of the amplitudes for these states are as follows: and �α� = = 1 16 for the amplitudes of states to the illustrative example which product to the state |1� .Then, the inversions about the mean of the amplitudes for these states are as follows: Hence, applying the Grover operator G can take this form: (e) Observe the system.
The following result is obtained after applying , G to the illustrative example.
3. Find the probability of a difference out of the R possible difference between N items as in Eq. (52).
Data: Given two Boolean functions f 1 and f 2 with n ≥ 0 inputs, N items and C is the number of differences.Result: Find the difference between f 1 and f 2 which makes f 1 evaluates to true and f 2 evaluates to false, i. e., find Go to step 2. end Algorithm 3.An Algorithm of Difference Operation.

The proposed quantum algorithm for union operation
Given two Boolean functions f 1 and f 2 , it is required to find the set of binary vectors that evaluates either f 1 or f 2 to True, i.e., to find the union between them.We need to find a partition of f 1 ∪ f 2 , thus decomposing it into a complement of the intersection of distinct complement sets, i. e. , such that f is a Boolean function which represents all the truth value of the states of the system, and f = f 1 ⊕ 1 ∩ f 2 ⊕ 1 .The steps of the proposed algorithm will be illustrated using the previous example: 1. Apply the algorithm in Sect.3.2 to find the result of f Applying the measurement on the auxiliary qubit to the illustrative example is as follows: To the illustrative example, the union that makes f 1 and f 2 evaluate to True can be obtained with the probability ( −1 √ 14 A pseudocode of union operation is shown in Algorithm 4; The circuit of the proposed algorithm is shown in Figs. 2 and 4.  www.nature.com/scientificreports/Data: Given two Boolean functions f 1 and f 2 with n ≥ 0 inputs, N items and C is the number of unions.Result: Find the union between f 1 and f 2 which makes them evaluate to true, i.e., find f 1 ∪ f 1 = ((f 1 ⊕ 1) ∩ ((f 2 ⊕ 1))) ⊕ 1 After applying algorithm 2, we can get the true values from the output state and consider it represents the true values of f , which will be used in the Younes et al. algorithm to find the complement of intersection in algorithm 2 as in the following steps.1. Prepare | ψ 0 = |0 ⊗n ⊗ |0 .2. Apply H on the first n qubits. 3. q y ← 1, N ← 2 n .4. while q y ≤ π

Dynamics of the proposed algorithms
Assume that R represents the number of possible solutions, α i (T) represents the amplitudes of desired (search- ing) of the stored data at a time step T such that 1 ≤ i ≤ R , β j (T) represents the amplitudes of non-solutions of the stored data such that R + 1 ≤ j ≤ m , γ k (T) represents the amplitudes of other data (which were not in the prepared superposition) such that m ≤ k ≤ N , α(T) represents the average of the amplitudes of desired (search- ing) of the stored data, β(T) represents the average of the amplitudes of non-solutions of the stored data, γ (T) represents the average of the amplitudes of other data (which were not in the prepared superposition).Then, we can define the form of the searching phase |ψ� as follows: and the average of the amplitudes are Let W(T) be the weighted averages over states After that, using Biron results 43 , the following relation holds:    f 1 using an amplitude amplification technique that employs entanglement and partial diffusion, while Arima's algorithm is used with an oracle that represents the second Boolean function f 2 to search for a solution that represents the result of the set operator using phase shift inversion about the mean.The proposed quantum algorithms can be used to apply set operations on two arbitrary Boolean functions in O √ N to find the solutions with high probability.The algorithms also demonstrated flexibility to compute arbitrary set relations compared to previous approaches.Resource estimates indicated polynomial number of qubits, suggesting potential near-term feasibility.Overall, the work developed practical quantum techniques advancing the capability to solve important set transformation problems.Future research includes optimizing resource costs, experiment implementation the algorithms, and customizing the approach to be used in different domains such as database queries, cryptography and machine learning.Furthermore, in the future, the proposed algorithms can be extended to perform set operations on more than two Boolean functions. https://doi.org/10.1038/s41598-024-56860-2 Apply the oracle operator I f T 1 on n + 1 qubits where I f T 1 evaluates the first boolean function f 1 as follows: https://doi.org/10.1038/s41598-024-56860-2

2 . 2 |ψ 4 �
Apply the Arima algorithm 21 as follows: Given: Phase oracles I f T 1 and I f T 2 and iterate the following for P = (π √ 2N )/8 times to find one match and P = (π √ N )/8 times to find more than one match. (a) |ψ 5 � = R f T When applying I f T 2

2 Figure 1 .
Figure 1.Quantum circuit for the proposed true intersection algorithm.
Apply the oracle operator I f T 1 on n + 1 qubits where I f T 1 evaluates the first boolean function f 1 as in Eqs. (

2 . 2 |ψ 5
Apply the Arima algorithm as follows Given: Phase oracles I f T 1 and I f T 2 and iterate for P = (π √ 2N )/8 times to find one match and P = (π √ N )/8 times to find more than one match the following (a) |ψ 6 � = I f T �.

2 √ 2 NC
Apply the oracle operator I f T 1 on n + 1 qubits

Figure 4 .
Figure 4.Quantum circuit to find the result of f ⊕ 1.
Finally, the first n qubits are measured to obtain one of the searched items. .:(0123456789) 1] n |r, 0 .Increment q y .end 5. Measure the extra qubit.6. if The outcome = |1 then Apply Z on the extra qubit.Apply H on the extra qubit.while r ≤ P = π An Algorithm of True Intersection Operation.
for the proposed difference algorithm.
Vol.:(0123456789) Scientific Reports | (2024) 14:7015 | https://doi.org/10.1038/s41598-024-56860-2www.nature.com/scientificreports/then the inversions about the mean of the amplitudes are .Hence, applying the partial diffusion D p can take this form (d) (d).Apply the measurement on the auxiliary qubit.The probability to get |1 � on the auxiliary qubit is C|c q | 2 and the superposition can be represented as follows: