I would like to make a real-time filter using Flink.
the idea is to have a value by key stored as accumulator and to calculate a ratio versus the total sum for all keys.
I know it's impossible to share state between keyed operator thus I'm not able to calculate the total value
example :
k1,1
k2,3
k1,1
k2,5
k3,0
I need to calculate on the stream the following ratio
1/1 , 3/4, 2/5, 8/10, 0 (is always filtered) etc...
Thanks for help
Create a custom stateful operator with the following state:
int totalSum;
Map<Key,Ratio> map;
Every event increments the total sum, then update the map according to the event key.
Example:
After 1st event k1,1 your state is:
totalSum 1
map
k1, 1/1
And you emit the event: k1, 1/1
======
After 2nd event k2,3 your state is:
totalSum 4
map
k1, 1/1
k2, 3/4
And you emit the event: k2, 3/4
[.. continue]
Related
What is the practical and theoretical difference is between these 3 states, which ultimately produce the same output result.
Could you tell me some examples of different results obtained starting from these 3 states and doing the same operations below.
The concept is unclear to me.
Thank you
|0> -> RY(pi/2) -> RX(pi) -> cnot q[0] q[1]
|0> -> RX(pi/2) -> cnot q[0] q[1]
|0> -> H -> cnot q[0] q[1]
Not all of these states are the same, assuming that you're talking about the single-qubit states obtained before application of the CNOT gate (otherwise please specify which single-qubit gates are applied to which qubit in the 2-qubit state).
The last state is H|0⟩ = 1/sqrt(2) (|0⟩ + |1⟩).
The first state ends up being the same state, up to a global phase, which means there is no way to observe a difference between these two states.
But the second state is 1/sqrt(2) (|0⟩ - i|1⟩), which behaves differently.
To observe the difference between the second and the last states, apply a Hadamard gate to both and measure them multiple times: you'll always get 0 result for the last state, but you'll get both 0 and 1 for the second state.
To quickly run this experiment, you can use Q#: running the following snippet will give you ~50 0 measurements for the state prepared using Rx and 100 0 measurements for the state prepared using H.
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Math;
operation RunTests (prep : (Qubit => Unit)) : Unit {
mutable n0 = 0;
use q = Qubit();
for _ in 1 .. 100 {
// Prepare the qubit in the given state.
prep(q);
// Apply Hadamard gate and measure.
H(q);
if M(q) == Zero {
set n0 += 1;
}
Reset(q);
}
Message($"{n0} zeros measured");
}
operation QubitsDemo () : Unit {
RunTests(Rx(PI() / 2.0, _));
RunTests(H);
}
By definition, the gate 1/sqrt(5) (I + 2iZ) should act on a qubit a|0> + b|1> to transform it into 1/sqrt(5) ((1+2i)a|0> + (1-2i)b|1>) but transformations of each RUS step does the following-
The ancillas are in |+> state at first
Starting form: 1/sqrt(2) (a,b,a,b,a,b,a,b)
CCNOT(ancillas, input): 1/sqrt(2) (a,b,a,b,a,b,b,a)
S(input): 1/sqrt(2) (a,ib,a,ib,a,ib,b,ia)
CCNOT(ancillas, input): 1/sqrt(2) (a,ib,a,ib,a,ib,ia,b)
Z(input) : 1/sqrt(2) (a,-ib,a,-ib,a,-ib,ia,-b)
Now measuring the ancillas in PauliX basis is equivalent to PauliZ measurement after applying H() to the state. Now I have 2 confusions, should I apply H x H x I or H x H x H to the combined state. Also neither of these transformations turn out to be equivalent to the V-gate defined in the first paragraph when both measurements are Zero. Where did I go wrong?
Reference: https://github.com/microsoft/Quantum/blob/master/samples/diagnostics/unit-testing/RepeatUntilSuccessCircuits.qs (1st sample code)
The transformation is correct, though it takes some time with pen and paper to verify it.
As a side note, we start with a state |+>|+>(a|0> + b|1>), which is 0.5 (a,b,a,b,a,b,a,b) in vector form (both |+> states contribute a 1/sqrt(2) to the coefficients). It will not affect our calculations of the state after the measurement, since it will have to be renormalized, but it's still worth noting.
After a sequence of CCNOT, S, CCNOT, Z we get 0.5 (a,-ib,a,-ib,a,-ib,ia,-b). Since we're measuring only the first two qubits in PauliX basis, we need to apply Hadamards only to the first two qubits, or H x H x I to the combined state.
I'll take the liberty to skip writing out the whole expression after applying Hadamards and fast-forward to the results of measurements, and here is why. We're only interested in the state of the input qubit if both measurements yielded 0, so it's sufficient to gather only the terms of the overall state which have |00> as the state of the first two qubits.
The state of the third qubit after measuring |00> on the first qubit will be: (3+i)a |0> - (3i+1)b |1>, multiplied by some normalization coefficient c.
c = 1/sqrt(|3+i|^2 + |3i+1|^2) = 1/sqrt(10)).
Now we need to check whether the state we got, |S_actual> = 1/sqrt(10) ((3+i)a |0> - (3i+1)b |1>)
is the same state as we'd expect to get from applying the V gate,
|S_expected> = 1/sqrt(5) ((1+2i)a |0> + (1-2i)b |1>). They do not look the same, but remember that in quantum computing the states are defined up to a global phase. Thus, if we can find a complex number p with an absolute value 1 for which |S_actual> = p * |S_expected>, the states will be effectively the same.
This translates into the following equations for p and amplitudes of |0> and |1>: (3+i)/sqrt(2) = p (1+2i) and -(3i+1)/sqrt(2) = p (1-2i). We solve both equations to get p = (1-i)/sqrt(2) which has indeed the absolute value 1.
Thus, we can conclude that indeed the state we got after all the transformations is indeed equivalent to the state we'd get by applying a V gate.
I recently came through an interesting coding problem, which is as follows:
There are n boxes, let us assume this is an array of n boxes.
For each index i of this array, three values are given -
1.) Weight(i)
2.) Left(i)
3.) Right(i)
left(i) means - if weight[i] is chosen, we are not allowed to choose left[i] elements from the left of this ith element.
Similarly, right[i] means if arr[i] is chosen, we are not allowed to choose right[i] elements from the right of it.
Example :
Weight[2] = 5
Left[2] = 1
Right[2] = 3
Then, if I pick element at position 2, I get weight of 5 units. But, I cannot pick elements at position {1} (due to left constraint). And cannot pick elements at position {3,4,5} (due to right constraint).
Objective - We need to calculate the maximum sum of the weights we can pick.
Sample Test Case :-
**Input: **
5
2 0 3
4 0 0
3 2 0
7 2 1
9 2 0
**Output: **
13
Note - First column is weights, Second column is left constraints, Third column is right constraints
I used Dynamic Programming approach(similar to Longest Increasing Subsequence) to reach a O(n^2) solution. But, not able to think of a O(n*logn) solution. (n can be up to 10^5.)
I also tried to use priority queue, in which elements with lower value of (right[i] + i) are given higher priority(assigned higher priority to element with lower value of "i", in case primary key value is equal). But, it is also giving timeout error.
Any other approach for this? or any optimization in priority queue method? I can post both of my codes if needed.
Thanks.
One approach is to use a binary indexed tree to create a data structure that makes it easy to do two operations in O(logn) time each:
Insert number into an array
Find maximum in a given range
We will use this data structure to hold the maximum weight that can be achieved by selecting box i along with an optimal selection of boxes to the left.
The key is that we will only insert values into this data structure when we reach a point where the right constraint has been met.
To find the best value for box i, we need to find the maximum value in the data structure for all points up to location i-left[i], which can be done in O(logn).
The final algorithm is to loop over i=0..n-1 and for each i:
Compute result for box i by finding maximum in range 0..(i-left[i])
Schedule the result to be added when we reach location i+right[i]
Add any previously scheduled results into our data structure
The final result is the maximum value in the whole data structure.
Overall, the complexity is o(nlogn) because each value of i results in one lookup and one update operation.
I have a state |Q> of n bits and want to measure the bit number i. Is there a matrix to apply on the state, so the state Q ends up to Q', like the Hadamard or X gates?
Or I should apply the measurement matrix |x><x| based on the outcome of the measurement, if 0 then x=0, and if 1 then x=1?
Although we often represent measurement as an operation that applies to a single qubit, it doesn't act like other single-qubit operations. There are some details omitted.
Equivalence w/ CNOT
Measuring a qubit is equivalent to using it as the control for a CNOT that toggles an otherwise unused ancilla qubit. Knowing this equivalence is useful, because it lets you translate what you know about two-qubit unitary operations into facts about measurement.
Here's a circuit showing that a qubit rotated around the Y axis ends up in the same mixed state when you measure as it does when you CNOT-onto-ancilla. The green circle things are Bloch sphere representations of each qubit's marginal state:
(If you want to use this CNOT trick to compute the mixed state result, instead of a pure state, just represent the state as a density matrix then trace over the ancilla qubit after performing the CNOT.)
Basically, measurement is observationally indistinguishable from making entangled copies. The difference, in practical terms, is that measurement is thermodynamically irreversible whereas a CNOT is easy to reverse.
Expected Outcomes
If you ignore the measurement result, then measurement acts like a projection of the density matrix. For example, in the animation above, notice that measurement causes the state to snap to (be projected onto) the Z axis of the Bloch sphere.
If you have access to the measurement result, then the measurement not only projects but also informs you of the new state of the system. In the single-qubit-in-the-computational-basis case, this forces the qubit to be all-ON or all-OFF due to the quantization of spin.
Representation
Measurements can be represented in various ways.
A very common representation is "projective measurements". Projective measurements are represented by a Hermitian matrix (called the "observable"). The eigenvalues of the matrix are the possible results. You get the probability of each result by projecting your state's density matrix into each eigenspace and tracing.
A more flexible and arguably better representation is positive-operator valued measures (POVM measurements). POVMs are represented by a set of squared Hermitian matrices, with the condition that the sum of the set's matrices must be the identity matrix. The probability of the result corresponding to the squared matrix F from the set is the trace of the state's density matrix times F.
Translating a projective measurement into a circuit that performs that measurement (using only computational basis measurements) is straightforward, because the necessary basis change operation is just a unitary matrix whose rows are the eigenvectors of the observable. Translating POVM measurements is trickier, and requires introducing ancilla bits.
For more information, see this answer on the physics stackexchange.
The measurement works as follows:
if you want to measure qubit number i (indexing from 1 to n), then based on the probability associated with all states, the outcome of measuring qubit i is 0 or 1 randomly with higher chance for the higher probability.
P_i(0) = <Q| M'0 M0 |Q>
P_i(1) = <Q| M'1 M1 |Q>
where P_i(0) is the probability of measuring qubit i to be 0, and P_i(1) is the probability of being 1. M0 is the measurment matrix of 0, and M1 is for 1. M'0 is M0 hermitian, and M'1 is M1 hermitian.
if you want to measure only the i-th qubit of the quantum system which is in state |Q> of n qubits. then the operation you would apply is:
I x I x I x I x ... x I x Mb x I x ... x I } n kronecker multiplication
1 2 3 4 ... i-1 i i+1 ... n } indices
where I is the identity matrix, Mb is the measurement matrix based on the measured value of the i-th either b=0, or b=1. x is the kronecker multiplication.
Summary:
pre measurement state |Q>
measurement of qubit i = b (b = 1 or 0 randomly selected based on the probability of each)
if b is 0: Mb = M0 = |0><0|
if b is 1: Mb = M1 = |1><1|
M = I x I x I x ... x I x Mb x I x ... x I
post state |Q'> = M|Q>
I have an array of int which contains numbers like {47, 94, 79, 90, 89, 14, 82, 92}. The array must be divided into three sub-arrays so that the sum of each array is the smallest possible, aka minimal. I think the its worth solving using recursion, however the approach escapes me, i also thought of using qsort on the initial array and then dividing it "greedily" but it doesnt work all the time (e.g taking the lowest and highest number and so on).
For example the numbers above would be divided into:
1) {94, 90, 14}
2) {92, 89}
3) {82, 79, 47}
Here the third array contains the highest minimal sum, which is 208. The order of the numbers does not matter. The question is how to fairly divide the numbers into three groups so that they form the lowest sum. Do I have to test all possibilities?
The described problem can be modelled using dynamic programming. We can define a state space as follows.
v[i,t1,t2] := minimal load in partition 3 attainable for items
in {0,...,i} where the total load in partition 1
is exactly t1 and the total load in t2 is exactly t2
if such a load exists and positive infinity otherwise
For the state space, i is in {0,...n}, and t1, t2 are in {0,...,P} where P is the total sum of the items, which is an upper bound for the objective value and is bounded by n*smax where smax is the largest value occuring in the input.
We obtain the following recurrence relation, where the cases basically depend on iteratively chosing for each element into which partition it is assigned, where s_i denotes the size of the i-th item.
v[i,t1,t2] = min { v[i-1,t1-s_i,t2],
v[i-1,t1,t2-s_i],
v[i-1,t1,t2] + s_i }
The first term in the minimum expression corresponds to assigning item i into the partition 3, the second case corresponds to assigning item i into partition 2 and the third case corresponds to assigning item i into the partition 3. After the state space is filled, the desired result (namely the minimal maximum load of the partition) can be obtained by evaluation of the following expression.
Result = min { max { t1, t2, v[n,t1,t2] : t1, t2 in {0,...,P} } }
In the maximum expression above, t1 would correspond to the load in partition 1, t2 would correspond to the load in partition 2 and the
state value v[n,t1,t2] corresponds to the load in partition 3. The running time of the sketched algorithm can be bounded by O(n^3*smax), which is a pseudopolynomial runtime bound. If additionaly the optimal assignment of the items into the partition is desired, either backtracking or auxiliary data structures have to be used.
Note that it seems artificial to give one of the identical partitions a special role as its load is the value of the states while the load of the other partitions is used for the axes of the state space. Furthermore, at first glance, the value of the state might seem to be trivially obtainable as it is simply the remaining total load
sum_{j=1}^{i} s_i - ( t1 + t2 )
but this is not the case, as the above quantity only determines the load in partition 3 if such an assignment actually exists; in the definition of the state space, the usage of positive infinity indicates the nonexistence of such an assignment.
The approach is very similar to the one described here, page 12 ff. In total, the described problem can be seen as a scheduling problem, namely minimization of the makespan of 3 identical parallel machines. In the so-called three-field notation, the problem is denoted as P3||Cmax, which means that the number of machines is not part of the input, but fixed.