Understanding Hadamard gate in depth - quantum-computing

I know that a Hadamard gate is implemented by rotating around the axis (x + z )/ sqrt(2), but how can I compute the matrix obtained by rotation around this axis by π radians, and compare to a Hadamard gate matrix.
Thanks for your help.

Using H = (x + z)/ sqrt(2)
Find the rotation along x-axis (rx), and rotation along z-axis (rz), then multiply.

Related

Calculating Displacement based on the compass direction

I need some help with programming the calculation of displacement. Given the distance the object has moved in the XY plane, and the yaw (heading) of the object, I'd like to calculate the displacement of the object. For example, the object moved 5m North and 2m East.
The data I have is the distance it travels in the XY plane (X distance and Y distance) and the heading of the device which is determined by the direction of X. I know how to calculate it on paper but I am having trouble to program it.
I've attached two examples that shows how I obtained the calculations. The black dot is the displacement of the object. In the 1st example, the X-distance travelled is 3 meters while the Y-distance is 4 meters and the heading of the X-axis is 70°. After calculating, I managed to get that it has travelled 2.733m South and 4.187m East. In example 2, it travel, the X-distance is -5m and the Y-distance is -12m, the heading is 160°. I calculated that the object travels 8.80m North and 9.566m East.
Example 1
Example 2
Assume you have your XY pane rotated by some angle Zp and have a 2D movement vector within that pane, let's say its direction rotated by Zv – what is total rotation of the vector, relative to the global orientation? Zp + Zv...
So all you need to do is applying the rotation Zp to the movement vector within XY-pane, i. e. apply a rotation matrix to the vector. This matrix for a rotation by z looks like:
cos(z) -sin(z)
sin(z) cos(z)
With our data above:
north = cos(Zp) * x - sin(Zp) * y
east = sin(Zp) * x + cos(Zp) * y
which gives, with your first sample data applied:
north = cos(70°) * 3 - sin(70°) * 4 ~ -2.7327
east = sin(70°) * 3 + cos(70°) * 4 ~ 4.1872
north = cos(160°) * -5 - sin(160°) * -12 ~ 8.8027
east = sin(160°) * -5 + cos(160°) * -12 ~ 9.5662
Corresponding pretty much to the values you calculated (note: negative movement to north is positive movement towards south...).
What you are doing is a conversion from polar to Cartesian coordinates. https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates
If your Cartesian coordinates are not aligned to the polar axis, just compensate by adding the rotation angle to the polar argument.

ImageDataGenerator how to change the range of the rotation?

It is possible to do a roation between $theta_{min}$ and $theta_{max}$?
ImageDataGenerator(rotation_range=90) rotate the images with a random angle between 0 and 90 degrees it is possible for example to rotate between 50 and 60 degrees?
Actually, Didier is right, 'rotation_range=90' means randomly rotate between [-90,90].
If you want to rotate for a fixed angle, I think you should try manually writing a function, which is corresponding to the parameter 'preprocessing_function' in ImageDataGenerator.
'preprocessing_function' could actually perform a lot of transforms, much flexible!
Hope this could help you!
In keras, ImageDataGenerator(rotation_range=90) does not rotate images with random angles between 0 degree and 90 degree. The random angle range is -90 degree and 90 degree.
# from ..../Anaconda3/Lib/site-packages/keras/preprocessing/image.py
if self.rotation_range:
theta = np.deg2rad(np.random.uniform(-self.rotation_range, self.rotation_range))
else:
theta = 0
What you want may be ImageDataGenerator(rotation_range=20) to random rotate images with random angles between -20 degree and 20 degree.

Simultaneous rotation in Matrix

Can anyone help me (again) please? I have a Matrix like this:
1.0 0.0 0.0 2.5
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0
How can I rotate it 20° in X axis, -128° in Y axis and 72.1° in Z axis simultaneously?
thank you very much
I want rotate … in X axis, … in Y axis and … in Z axis simultaneously
You can't. What you ask for is mathematically undefined. There are 6 permutations of the order in which the elementary rotations could be combined…
X Y Z
X Z Y
Y X Z
Y Z X
Z X Y
Z Y X
and each of them has a different result. Rotations don't work the way you think. Mathematically rotations in 3 dimensional space form a special unitary group of degree 2, also written as SU(2). Each rotation in SU(2) is unique but can be constructed by combining an infinite number of other rotations in SU(2).
In your particular case there's no particular solution to the problem. The best thing you can do is choose a particular execution order and apply the rotations one after another onto your existing coordinate system, by forming the corresponding rotation matrix and multiplying onto the matrix representing the previous coordinate system/transformation step.

Calculating an index in an array help with algorithm

I'm taking a sprite sheet and splitting it up into an array. I then take that array and rotate the image by a certain granularity and push that onto the end of the array too to pre bake the rotations for faster drawing (no hardware acceleration). The final result looks something like this:
array[0] = frame1 of the animation
array[1] = frame2 of the animation
array[2] = frame3 of the animation
array[3] = frame1 of the animation rotated at 45 degrees
array[4] = frame2 of the animation rotated at 45 degrees
array[5] = frame3 of the animation rotated at 45 degrees
array[6] = frame1 of the animation rotated at 90 degrees
... etc
So now I'm trying to come up with a function that'll return the right element for any angle at the right granularity. For example, say my in-game object is currently rotated 30 degrees and is on frame1 of the animation, I would want array[3]. If I was on frame2 of the animation, I would want array[4]. If the object was rotated 80 degrees and was on frame1, I would want array[6], and so on.
These are the constants I have:
The # of animation frames (pre-rotation)
The # of animation frames (post-rotation)
The Granularity of the Rotations (from 1 to 360, ie granularity of 45 would be the above example).
This is the signature of the function to calculate the frame:
function calculateImageIndex(animationFrame : Number, currentAngle : Number) : Number
Any ideas on how to calculate this? I'm stumped.
nf: total number of frames
gr: granularity of rotation
r: actual angle of rotation
f: actual frame number
i = round(r / gr) * nf + f - 1
n = Number of frames (Pre-rotation)
g = granularity of rotation
r = actual angle of rotation
f = actual frame
I think these are the constants you need and are given.
The first thing we will want to do it find the number of rotations it would take to get to the angle desired. In the above example getting to an angle of 45 would be 1 rotation. 90 degrees = 2 rotations and so on. Let R = number of rotations.
R = r / g This should always be an integer as you should never need an angle that doesn't fit the gratuity you used.
Next we will calculate the starting index of that rotation "group". In your example above the first group with a rotation 0 would start at index 0. 45 degress or 1 rotation would start at index 3. And so on. To do this we need to multiply the number of rotations (R) by the number of frames pre-rotation (n). Let j = the starting index of that rotation group.
j = R * n
The last step would be to figure out how much you must add to the starting index (j) to reach the frame you want. I will assume the first frame will be numbered 1 as in your example above, but if the first frame is numbered 0 then remove the -1 in the algorithm. Let i = the final index.
i = j + (f - 1)
I will be the index you are looking for. To put this together in one algorithm it would look like this.
i = ((r / g) * n ) + (f - 1)
Hope this helps! Let me know if you need my to clarify anything.

generating/creating hexagon grid in C

So i m trying to make hexagonal grid in C for a game. I am really dumb founded on where to start on it. ANyone have any ideas.
EDIT: I need about 15-20 hexagons in a grip shape all joined,something like a game board. for a game i m working on. Sorry for not being clear
Absolutely. Despite their odd shape, hexagons can still be contained in your usual multidimensional array, for future use (I assume you'll want to put things in your hexagons). As for drawing them, it's simple. Sum of angles = (6 - 2) * 180 = 4 * 180 = 720. One angle is 720 / 6 = 120 degrees. Calculate first the leftmost angle's Y position, which is equal to √(hexagonSide - hexagonWidth * hexagonWidth). I'm sure you can figure out hexagonWidth, right? Okay, now the X position relative to the last one will be 0. You'll need to offset the Y position by half the height of the hexagon before it, up or down depending on whether row * col is even or odd. Since you know the hexagon's width you have the coordinates of the opposite angle. Rotate by 120° and repeat.
Before I continue, is this supposed to be in the console? Or is it real graphics?
So let's get this straight, the game will be played on the console? Right, well now you will need to set up your data structures, the most obvious is with nodes.
The nodes
Each hexagon is a node with six edges.
typedef struct Node {
void *object; /* Pointer to object */
Node *node_array; /* Pointer to node_array with 'node_count' nodes */
int node_count; /* size of node_array */
} Node;
How to initialize and connect the node structure
Imagine the following hexagon:
/\
| |
\/
It has the following edges, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST, WEST and NORTHWEST. Next observe how they will be arranged (10, 11 and 12 were represented in Hex so that they can fit in one space):
// 0 1 2 3
// 4 5 6 7 8
// 9 A B C
So 0 will link to 5 through it's SOUTHEAST link, and 4 through it's SOUTHWEST link. Also notice how the rows alternate between odd and even numbers of elements. Let's call {0, 1, 2, 3} row[0], and {4, 5, 6, 7, 8} row[1]. And let's call this a 5x3 hexmap. The easiest way to create this array is with malloc(sizeof(Node) * width * height).
Connecting the nodes
First of all let me point out that every even row (0, 2, 4, ...) will have width-1 elements. But there's more, each element (x, y) on this row will link to the following element in your array:
(x+1, y-1) - NORTHEAST
(x+1, y) - EAST
(x+1, y+1) - SOUTHEAST
(x, y+1) - SOUTHWEST
(x-1, y) - WEST
(x, y-1) - NORTHWEST
Elements on the other rows, such as {4, 5, 6, 7, 8} will have width elements, where element (x, y) links to the following:
(x, y-1) - NORTHEAST
(x+1, y) - EAST
(x, y+1) - SOUTHEAST
(x-1, y+1) - SOUTHWEST
(x-1, y) - WEST
(x-1, y-1) - NORTHWEST
When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width and 0 <= y < height.
Remember ...
Your array contains one unused element at the end of every two rows (row[0], row[2], etc.). Also you might want to provide them all with some sort of label or index so that you can refer the player to them. You could label them as (x,y) pairs, or numerically by their index, it's all up to you. The (x, y) pair is very easy for you since that will map directly to the array they are stored in.

Resources