Microsoft Q# : problems with rotate function R() - q#

Recently I've been working on algorithms of quantum computing where I faced some problems.
As there are probabilities for qubits to be observed as |1> or |0>, the rotation function is supposed to change the status of each qubit so that there can be more or fewer chances for them to be measured in a specific status.
In my program, I applied Hadamard transformation to a qubit and then rotated it with an angle θ(90°) in the z-axis, which was to make an equal probability of |0> and |1> measurement and then the rotation made the probs of status |1> 100%, but it didn't work anyway. The probs for |1> will always be 50% which is not supposed to be.
Here is my code:
operation Testing () : (Int)
{
body
{
mutable result = -1;
using (qubit = Qubit[1])
{
H(qubit[0]);
R(PauliZ, PI() / -2.0, qubit[0]);
//assertprob is a function to help test the probabilities of qubits in specific status
AssertProb([PauliZ], qubit, One, 0.5,"Measuring in conjugate basis did not give 50/50 results.", 1e-5);
//AssertProb([PauliZ], qubit, Zero, 0.5,"Measuring in conjugate basis did not give 50/50 results.", 1e-5);
let value = M(qubit[0]);
if(value == One)
{
set result = 1;
} else {
set result = 0;
}
ResetAll(qubit);
}
return result;
}
}
A photo from Microsoft Q# documentation may help explain the sphere structure. After the Hadamard transformation, the qubit is in status [1/√2,1/√2] and then the rotation of -π/2 makes it to status [0,1].
(source: microsoft.com)

Applying a rotation around the Pauli Z axis doesn't change the measurement probabilities of Z-axis measurements. You can visualize it this way:
Your qubit's initial state is at +1 on the Z axis.
The H operation moves it onto +1 on the X axis.
Any rotation around the Z axis keeps the qubit's state in the X-Y plane, which is equally likely to measure as 0 or as 1 in the Z axis.
A pi/2 or 3pi/2 rotation around the Y axis will get the state back to the Z axis.

Related

Gravity in accelerometric measurements

I have taken from a data set the values ​​of x and z of activity (e.g. walking, running) detected by an accelerometer. Since the data collected also contains the gravity values, I removed it with the following filter in Matlab:
fc = 0.3;
fs = 50;
x = ...;
y = ...;
z = ...;
[but,att] = butter(6,fc/(fs/2));
gx = filter(but,att,x);
gy = filter(but,att,y);
gz = filter(but,att,z);
new_x = x-gx;
new_y = y-gy;
new_z = z-gz;
A = magnitude(new_x,new_y,new_z);
plot(A)
Then I calculated the magnitude value and plotted the magnitude value on a graph.
However, every graph, even after removing gravity, starts with a magnitude of 1g (9.8 m / s ^ 2), why? Should not it start at 0 since I removed gravity?
You need to wait for the filter value to ramp up. Include some additional data that you don't graph at the beginning of the file for this purpose.
How accurate do your calculations need to be? With walking and running the angle of the accelerometer can change, so the orientation of the gravity vector can change throughout the gait cycle. How much of a change in orientation you can expect to see depends on the sensor location and the particular motion you are trying to capture.

Calculating a spiral in MATLAB

We have these logarithmic spirals which are circling around the centre of the coordinate system:
x = ebθ cos(θ)
y = ebθ sin(θ)
where the ebθ is the distance between the point (which is on the spiral) and the centre; and the θ is the angle between the line connecting the point and the origin and the axis x.
Consider a spiral where the angle is θ ϵ <0,10π> and the parameter is b=0.1. By thickening points on the spirals (and the angle θ) calculate the circumference with the relative precision better than 1%. Draw the spiral!
I'm preparing for a (MATLAB) test and I'm stuck with this exercise. Please help, any hint is appreciated.
Start by computing a list of x,y for your range of theta and value of b. For more accurate results, have your theta increment in smaller steps (I chose 5000 arbitrarily). Then, its simply computing the distance for each pair of consecutive points and summing them up.
t = linspace(0,10*pi,5000);
b = 0.1;
x = exp(b*t).*cos(t);
y = exp(b*t).*sin(t);
result = sum(sqrt((x(2:end) - x(1:end-1)).^2 + (y(2:end)-y(1:end-1)).^2))

Dome rotation on arbitrary axis?

Imagine a dome with its centre in the +z direction. What I want to do is to move that dome's centre to a different axis (e.g. 20 degrees x axis, 20 degrees y axis, 20 degrees z axis). How can I do that ? Any hint/tip helps.
Add more info:
I've been dabbling with rotation matrices in wiki for a while. The problem is, it is not a commutative operation. RxRyRz is not same as RzRyRx. So based on the way I multiple it I get a different final results. For example, I want my final projection to have 20 degrees from the original X axis, 20 degrees from original Y axis and 20 degrees from original Z axis. Based on the matrix, giving alpha, beta, gamma values 20 (or its corresponding radian) does NOT result the intended rotation. Am I missing something? Is there a matrix that I can just put the intended angles and get it at the end ?
Using a rotation matrix is an easy way to rotate a collection of (x,y,z) points. You can calculate a rotation matrix for your case using the equations in the general rotation section. Note that figuring out the angle values to plug into those equations can be tricky. Think of it as rotating about one axis at a time and remember that the order of your rotations (order of multiplications) does matter.
An alternative to the general rotation equations is to calculate a rotation matrix from axis and angle. It may be easier for you to define correct parameters with this method.
Update: After perusing Wikipedia, I found a simple way to calculate rotation axis and angle between two vectors. Just fill in your starting and ending vectors for a and b here:
a = [0.0 0.0 1.0];
b = [0.5 0.5 0.0];
vectorMag = #(x) sqrt(sum(x.^2));
rotAngle = acos(dot(a,b) / (vectorMag(a) * vectorMag(b)))
rotAxis = cross(a,b)
rotAxis =
-0.5 0.5 0
rotAngle =
1.5708

Getting the derivative of a Bezier curve problem in Cocos2d

In cocos2d you can move a sprite in a Bezier path using ccBezierConfig. That's not my problem though, I have a missile and am trying to make it rotate perpendicular to that point on the curve. I couldn't figure it out for a while and then my friend told me about derivatives. Now I need to find the derivative of a bezier curve apparently. I searched on Google and found it on this page: http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html. So then I tried implementing rotating the missile with 3 methods here they are:
-(float)f:(int)x {
if (x == 0)
return 1.0f;
float result = 1;
while (x>0) {
result = result*x;
x--;
}
return result;
}
-(float)B:(float)u i:(int)i n:(int)n {
return ([self f:n])/([self f:i]*[self f:(n-i)])*pow(u, (float)i)*pow(1-u, (float)n-i);
}
-(void)rotateMissile:(float)delta {
//Get bezier derivative...
float y = [self B:missileP1.controlPoint_1.x i:0 n:2]+[self B:missileP1.controlPoint_2.x i:1 n:2]
*2*(missileP1.controlPoint_1.x - missileP1.controlPoint_2.x);
//Take the y and rotate it...
missile1.rotation = atanf(y);
}
The first method is for factorials, the second is supposed to find B in the equation derivative. The 3rd method is supposed to find the actual derivative and rotate the missile by converting slope to degrees using atanf.
The rotateMissile is being called continuously like such:
[self schedule:#selector(rotateMissile:)];
missileP1 is the ccBezierConfig object. missile1 is the missile I am trying to rotate. I'm just really confused about this whole derivative thing (in other words, I'm really lost and confused). I need help trying to figure out whats wrong... Sorry that the code is messy, the equations were long and I could figure out a way to make it less messy.
Actually I don't understand how are you taking a derivative and putting it into a float. That's because Bizier curve is two dimensional parametric curve (it has x and y components). It is not a function y(x). In cubic case it is:
x(t) = x0 + x1*t + x2*t*t + x3*t*t*t
y(t) = y0 + y1*t + y2*t*t + y3*t*t*t
Let's call it form1. So actually it's nothing more then two polynomials of third order. The traditional form of cubic Bezier curve is
Note, that B(t) here is a two dimensional vector (x(t), y(t)). So if you have a tradional way defined Bezier curve you can convert it to form1 by evaluating coefficients x0, x1 and son on.
If you now have your Bezier curve defined in form1 it is very easy to take the derivative:
x'(t) = x1 + 2*x2*t + 3*x3*t*t
y'(t) = y1 + 2*y2*t + 3*y3*t*t
Now the vector (x'(t), y'(t)) - is the velocity on your bezier curve. It also a tangent vector to your curve. The perpendicular vector will be (-y'(t), x'(t)) or ((y'(t), -(x'(t)).
Here are the coefficients:
For y coefficients the formula is totally identical. It just will py0, py1, py2, py3 instead of px0, ... .

How can I correctly calculate the direction for a moving object?

I'm solving the following problem: I have an object and I know its position now and its position 300ms ago. I assume the object is moving. I have a point to which I want the object to get.
What I need is to get the angle from my current object to the destination point in such a format that I know whether to turn left or right.
The idea is to assume the current angle from the last known position and the current position.
I'm trying to solve this in MATLAB. I've tried using several variations with atan2 but either I get the wrong angle in some situations (like when my object is going in circles) or I get the wrong angle in all situations.
Examples of code that screws up:
a = new - old;
b = dest - new;
alpha = atan2(a(2) - b(2), a(1) - b(1);
where new is the current position (eg. x = 40; y = 60; new = [x y];), old is the 300ms old position and dest is the destination point.
Edit
Here's a picture to demonstrate the problem with a few examples:
In the above image there are a few points plotted and annotated. The black line indicates our estimated current facing of the object.
If the destination point is dest1 I would expect an angle of about 88°.
If the destination point is dest2 I would expect an angle of about 110°.
If the destination point is dest3 I would expect an angle of about -80°.
Firstly, you need to note the scale on the sample graph you show above. The x-axis ticks move in steps of 1, and the y-axis ticks move in steps of 20. The picture with the two axes appropriately scaled (like with the command axis equal) would be a lot narrower than you have, so the angles you expect to get are not right. The expected angles will be close to right angles, just a few degrees off from 90 degrees.
The equation Nathan derives is valid for column vector inputs a and b:
theta = acos(a'*b/(sqrt(a'*a) * sqrt(b'*b)));
If you want to change this equation to work with row vectors, you would have to switch the transpose operator in both the calculation of the dot product as well as the norms, like so:
theta = acos(a*b'/(sqrt(a*a') * sqrt(b*b')));
As an alternative, you could just use the functions DOT and NORM:
theta = acos(dot(a,b)/(norm(a)*norm(b)));
Finally, you have to account for the direction, i.e. whether the angle should be positive (turn clockwise) or negative (turn counter-clockwise). You can do this by computing the sign of the z component for the cross product of b and a. If it's positive, the angle should be positive. If it's negative, the angle should be negative. Using the function SIGN, our new equation becomes:
theta = sign(b(1)*a(2)-b(2)*a(1)) * acos(dot(a,b)/(norm(a)*norm(b)));
For your examples, the above equation gives an angle of 88.85, 92.15, and -88.57 for your three points dest1, dest2, and dest3.
NOTE: One special case you will need to be aware of is if your object is moving directly away from the destination point, i.e. if the angle between a and b is 180 degrees. In such a case you will have to pick an arbitrary turn direction (left or right) and a number of degrees to turn (180 would be ideal ;) ). Here's one way you could account for this condition using the function EPS:
theta = acos(dot(a,b)/(norm(a)*norm(b))); %# Compute theta
if abs(theta-pi) < eps %# Check if theta is within some tolerance of pi
%# Pick your own turn direction and amount here
else
theta = sign(b(1)*a(2)-b(2)*a(1))*theta; %# Find turn direction
end
You can try using the dot-product of the vectors.
Define the vectors 'a' and 'b' as:
a = new - old;
b = dest - new;
and use the fact that the dot product is:
a dot b = norm2(a) * norm2(b) * cos(theta)
where theta is the angle between two vectors, and you get:
cos(theta) = (a dot b)/ (norm2(a) * norm2(b))
The best way to calculate a dot b, assuming they are column vectors, is like this:
a_dot_b = a'*b;
and:
norm2(a) = sqrt(a'*a);
so you get:
cos(theta) = a'*b/(sqrt((a'*a)) * sqrt((b'*b)))
Depending on the sign of the cosine you either go left or right
Essentially you have a line defined by the points old and new and wish to determine if dest is on right or the left of that line? In which case have a look at this previous question.