Effect of S-gate on one qubit of a combined(maybe entangled) state of 3 qubits - quantum-computing

Suppose I have a register(qs) of 3 qubits (first 2 being used solely for control, the last one is the input) . The first two control qubits are in the |+> state and the state of the 3rd input is unknown. Let it be a|0> + b|1>.
Now I apply CCNOT(qs[0],qs[1],qs[2]) so their combined state becomes 0.5(a,b,a,b,a,b,b,a) in Transposed matrix form [Please correct if I'm wrong here] . Now I apply S-gate to the 3rd qubit which transforms |1> -> i|1> .
I am unable to guess the state of the combined state of 'qs' now.
What I thought:
One logic is to multiply every state by 'i' if it has the form|XY1> so the combined state becomes 0.5(a,ib,a,ib,a,ib,b,ia) [Transposed]
Another logic is to find tensor product of (I x I x S) since I'm not changing the first 2 qubits. Performing this yields a different result which is 0.5(a,b,a,b,ia,ib,ib,ia) [Transposed] [Again, correct me if I'm wrong].
Which is the correct output after passing through S-gate (if any) ?

The first two qubits can't start in |+> state, since |+> is a single-qubit state. I assume that the starting state of the first two qubits in the register is 0.5 (|00> + |01> + |10> + |11>).
Both approaches are correct, because they are different ways to represent the same transformation. The first answer 0.5(a,ib,a,ib,a,ib,b,ia) [Transposed] is correct. Your second answer 0.5(a,b,a,b,ia,ib,ib,ia) [Transposed] seems to be obtained by multiplying by S x I x I, i.e., applying S gate on the first qubit instead of the third one.
The tensor product I x I x S can be calculated as tensor product of I x I (which is just a 4x4 identity matrix) and S. The result is an 8x8 matrix which consists of 16 copies of S matrix, multiplied by corresponding elements of I x I:
1 0 | 0 0 | 0 0 | 0 0
0 i | 0 0 | 0 0 | 0 0
- - - - - - - -
0 0 | 1 0 | 0 0 | 0 0
0 0 | 0 i | 0 0 | 0 0
- - - - - - - -
0 0 | 0 0 | 1 0 | 0 0
0 0 | 0 0 | 0 i | 0 0
- - - - - - - -
0 0 | 0 0 | 0 0 | 1 0
0 0 | 0 0 | 0 0 | 0 i
If you multiply the state of the qubits by this matrix, you'll get the same answer as in the first approach.

Related

Difference between density matrix and completeness relation

The expression for completeness relation in quantum mechanics is -
Σ |ψ_n><ψ_n| = 1
where the expression for density matrix in statistical mechanics is -
ρ = Σ p_n |ψ_n><ψ_n|
Both of the equation looks the same. So what are the differences between the density matrix and completeness relation?
What is the basic difference between them?
Formally the difference is that for the density matrix there are pre-factors p_n which sum up to 1 rather than all being 1 as in the completeness relation.
The meaning is also quite different.
Here is a rough illustration what they mean:
This object is a projection operator:
|ψ_n><ψ_n|
It projects on the n-th basis vector.
For simplicity lets take a simple example. Say our Hilbert space is 3 dimensional. Then the sum runs from 1 to 3. Each so-called pure state can be represented by a vector of length 1 in a 3 dimensional space like these examples:
|ψ_1> = (1, 0, 0)T
|ψ_2> = (0, 1, 0)T
|ψ_3> = (0, 0, 1)T
|φ> := (0, 1/2^0.5, 1/2^0.5)T
(The "T" stands for transposed)
These projection operators can be written as a matrix like for example:
/ 0 0 0 \
|ψ_2><ψ_2| = | 0 1 0 |
\ 0 0 0 /
Now what these projection operators do is projecting a vector on one of the coordinate axes. E.g. for n=2 we project to the y-axis.
|ψ_2><ψ_2|φ> = (0, 1/2^0.5, 0)
Now what the completeness relation says is that the sum of those 3 vectors you get when projection on each coordinate axis is once again the original vector (see Basis Decomposition).
As this is true for any vector, this means the operation is the identity matrix:
/ 1 0 0 \ + / 0 0 0 \ + / 0 0 0 \ / 1 0 0 \
|ψ_1><ψ_1| + |ψ_2><ψ_2| + |ψ_3><ψ_3| = | 0 0 0 | + | 0 1 0 | + | 0 0 0 | = | 0 1 0 | = 1
\ 0 0 0 / + \ 0 0 0 / + \ 0 0 1 / \ 0 0 1 /
Now the density matrix is a completely different matter. The weights p_n describe how one state is a mixture of several "pure" states. See e.g. https://en.wikipedia.org/wiki/Density_matrix

Efficient way to "fill" a binary matrix to create smooth, connected zones

I have a large matrix of 1's and 0's, and am looking for a way to "fill" up areas that are locally dense with 1's.
I first did this task for an array, and counted the number of 1's within a certain radius of the element in questions. If the radius was 5, for example, and my threshold was 4, then a point that had 4 elements marked "1" within 5 elements to the left or right would be changed to a 1.
Basically I would like to generalized this to a two - dimensional array and have a resulting matrix that has "smooth" and "connected" regions of 1's and no "patchy" spots.
As an example, the matrix
1 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 1 0 0 0
0 0 1 1 1 0 0
would ideally be changed to
1 0 0 1 1 0 0
0 0 1 1 1 0 0
0 1 1 1 1 0 0
0 0 1 1 1 0 0
or something similar
For binary images, the morphologial operations that are implemented in MATLAB are perfect for manipulating the shape and size of connected regions. Specifically, the process of image closing is designed to fill holes in connected regions. In MATLAB, the function is imclose, which takes the image and a structuring element, similar to a filter kernel, for how neighboring pixels effect the filling of holes and gaps. A simple invocation of imclose is,
IM2 = imclose(IM,strel(ones(3)));
Larger gaps can be filled by increasing the area of the influence of of neighboring pixes, via larger structuring elements. For example, we an use a disk of radius 10 pixels:
IM2 = imclose(IM,strel('disk',10));
While, imclose supports grayscale and binary (0 and 1) images, the function bwmorph is designed for operation on binary images only but provides a generic interface to all of the morphological operations and various neat combinations of operations (e.g. 'bothat', 'tophat', etc.). The syntax for closing is simplified with bwmorph:
BW2 = bwmorph(BW,'close');
Here the structuring element is the standard ones(3).
A simple filter such as the following might do the trick:
h = [ 0 1 0
1 0 1
0 1 0];
img2=(imfilter(img,h)>2) | img;
For instance:
img =
1 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 1 0 0 0
0 0 1 1 1 0 0
img2 =
1 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 0 0
0 0 1 1 1 0 0
You can try different filters to modify the output img2.
This uses the image processing toolbox. If you don't have that, you may want to look up equivalent routines from the matlab exchange.

C Programming - XOR Bitwise Operation

What operation does the following ‘C’ statement perform?
star = star ^ 0b00100100;
(A) Toggles bits 2 and 5 of the variable star.
(B) Clears all bits except bits 2 and 5 of the variable star.
(C) Sets all bits except bits 2 and 5 of the variable star.
(D) Multiplies value in the variable star with 0b00100100.
I'm still clueless about this. Can someone help me out?
XOR operator (also called "logical addition") is defined like this:
a b a^b
-----------
0 0 0
0 1 1
1 0 1
1 1 0
So a^0 leaves a intact while a^1 toggles it.
For multiple-bit values, the operation is performed bitwise, i.e. between corresponding bits of the operands.
If you know how XOR works, and you know that ^ is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.
From an "during the test" standpoint, let's say you need to prove this to yourself, you really don't need to know the initial value of star to answer the question, If you know how ^ works then just throw anything in there:
00100100
^10101010 (star's made up value)
---------
10001110 (star's new value)
bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
|---|---|---|---|---|---|---|---
star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
|---|---|---|---|---|---|---|---
star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0
Then check your answers again, did it:
(A) Toggles bits 2 and 5 of the variable star. (Yes)
(B) Clears all bits except bits 2 and 5 of the variable star. (Nope)
(C) Sets all bits except bits 2 and 5 of the variable star. (Nope)
(D) Multiplies value in the variable star with 0b00100100. (36x170 = 142? Nope)
It is (A) toggles bits 2 and 5.
The following is the truth table for the XOR operation:
x y x^y
0 0 0
1 0 1
0 1 1
1 1 0
You can see from the table that x XOR 0 = x and x XOR 1 = !x.
XOR is a bitwise operation, so it operates on individual bits. Therefore if you XOR star with some constant, it will toggle the 1 bits in the constant.
You can find some explanation e.g. here.
The exclusive OR has this truth table:
A B A^B
-----------
1 1 0
1 0 1
0 1 1
0 0 0
We can see that if B is true (1) then A is flipped (toggled), and if it's false (0) A is left alone. So the answer is (A).
XOR operator returns 0 if both inputs are same otherwise returns 1 if both inputs are different.For Example the Given Truth Table :-
a=1 b=1 => a^b=0,
a=0 b=0 => a^b=0,
a=0 b=1 => a^b=1,
a=1 b=0 => a^b=1.
well xor is binary operator that work on bits of 2 nos.
rule of xoring:for same bit ans is 0 and for different bit ans is 1
let
a= 1 0 1 0 1 1
b= 0 1 1 0 1 0
--------------
c= 1 1 0 0 0 1
--------------
compare bit of a and b bit by bit
if same put 0 else put 1
xor is basically used to find the unique in given set of duplicate no.
just xor all nos. and u will get the unique one(if only single unique is present)

Find out the column number in which Maxima occur in a matrix in Mata in a for-loop

My purpose is not to find out what the maximum value is but the column number(S) in which the maximum occurs when generated different random numbers ~N to the entire column elements of the row.
A potential problem here is what if two or more elements in the same row contain the same value which is the maximum? I know it is unlikely for they content are real numbers. But I preset all elements in first row to be zero. Therefore, in the first round, all values are maximum and are subject to next conditional action.
I will want the column number(s) to do further computation in the next round of loop
Suppose all the matrix are well defined before the for-loop to save space here.
Mata:
for (k=1; k<=10; k++){
for (j=1; j<=20; i++){
A[k,j]= sum[k,j] \ count [1,j]
}
Choose max A[k,j]
For that j* for max A[k,j*] to occur
{count[1,j*]=count[1, j*+1]
y= rnormal(1,1,x,5)
C[k,j*]=y
sum[k,j*]=sum[k,j*]+c[k,j*]
}
}
Thank you so much for your help.
I can't follow most of this -- a lot seems to be background tangential to your question -- but if the question is to find the column index of the largest element in a row vector, here is one technique:
: y = rnormal(1,10,0,1)
: y
1 2 3 4 5
+----------------------------------------------------------------------------
1 | .3660763572 .4824003439 -.4441589685 .4314199623 -1.422563986
+----------------------------------------------------------------------------
6 7 8 9 10
----------------------------------------------------------------------------+
1 -1.226056129 1.18790502 -.4106889581 1.024620896 1.092570732 |
----------------------------------------------------------------------------+
: select((1..10), (y :== max(y)))
7
(LATER)
I haven't tried to grasp the whole of what you are trying to do, but you are making very bold assertions about a language you have only just started to learn.
Generally, select() will take a matrix as first argument.
Specifically, there is no problem in identifying the maximum in a matrix.
: y = rnormal(5,5,0,1)
: max(y)
2.001071729
: y :== max(y)
1 2 3 4 5
+---------------------+
1 | 0 0 0 0 0 |
2 | 0 0 0 0 1 |
3 | 0 0 0 0 0 |
4 | 0 0 0 0 0 |
5 | 0 0 0 0 0 |
+---------------------+
I hope that helps a bit.
for (k=2; k<=n; k++){
for (j=1; j<=50; j++){
Ri[k,j]= sumAi[1,j] / count[1,j]
}
Maxj= select((1..50), (Ri[k,]:==max(Ri[k,])))
count[1,Maxj]=count[1,Maxj]+1
y= rnormal(1,1,x,5)
Ai[k,Maxj]=y
sumAi[1,Maxj]=sumAi[1,Maxj]+y
Maxj=.
}

ANSI C + Numerical Linear Algebra - Using a linear solver to find an eigenvector given an eigenvalue (issue)

I have written a linear solver employing Householder reflections/transformations in ANSI C which solves Ax=b given A and b. I want to use it to find the eigenvector associated with an eigenvalue, like this:
(A-lambda*I)x = 0
The problem is that the 0 vector is always the solution that I get (before someone says it, yes I have the correct eigenvalue with 100% certainty).
Here's an example which pretty accurately illustrates the issue:
Given A-lambda*I (example just happens to be Hermitian):
1 2 0 | 0
2 1 4 | 0
0 4 1 | 0
Householder reflections/transformation will yield something like this
# # # | 0
0 # # | 0
0 0 # | 0
Back substitution will find that solution is {0,0,0}, obviously.
It's been a while since I've written an eigensolver, but I seem to recall that the trick was to refactor it from (A - lambda*I) * x = 0 to A*x = lambda*x. Then your Householder or Givens steps will give you something like:
# # # | #
0 # # | #
0 0 1 | 1
...from which you can back substitute without reaching the degenerate 0 vector. Usually you'll want to deliver x in normalized form as well.
My memory is quite rusty here, so I'd recommend checking Golub & Van Loan for the definitive answer. There are quite a few tricks involved in getting this to work robustly, particularly for the non-symmetric case.
This is basically the same answer as #Drew, but explained a bit differently.
If A is the matrix
1 2 0
2 1 4
0 4 1
then the eigenvalues are lambda = 1, 1+sqrt(20), 1-sqrt(20). Let us take for simplicity lambda = 1. Then the augmented matrix for the system (A - lambda*I) * x = 0 is
0 2 0 | 0
2 0 4 | 0
0 4 0 | 0
Now you do the Householder / Givens to reduce it to upper triangular form. As you say, you get something of the form
# # # | 0
0 # # | 0
0 0 # | 0
However, the last # should be zero (or almost zero). Exactly what you get depends on the details of the transformations you do, but if I do it by hand I get
2 0 4 | 0
0 2 0 | 0
0 0 0 | 0
Now you do backsubstitution. In the first step, you solve the equation in the last row. However, this equation does not yield any information, so you can set x[2] (the last element of the vector x) to any value you want. If you set it to zero and continue the back-substitution with that value, you get the zero vector. If you set it to one (or any nonzero value), you get a nonzero vector. The idea behind Drew's answer is to replace the last row with 0 0 1 | 1 which sets x[2] to 1.
Round-off error means that the last #, which should be zero, is probably not quite zero but some small value like 1e-16. This can be ignored: just take it as zero and set x[2] to one.
Obligatory warning: I assume you are implementing this for fun or educational purposes. If you need to find eigenvectors in serious code, you are better off using code written by others as this stuff is tricky to get right.

Resources