forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Moving quaternions around * Linting * Update unitary.py * Update qiskit/quantum_info/operators/unitary.py Co-Authored-By: jaygambetta <[email protected]>
- Loading branch information
1 parent
fed597d
commit 7b5b99a
Showing
4 changed files
with
55 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2018, IBM. | ||
# | ||
# This source code is licensed under the Apache License, Version 2.0 found in | ||
# the LICENSE.txt file in the root directory of this source tree. | ||
|
||
""" | ||
A module for common unitary matrix. | ||
""" | ||
import math | ||
import numpy as np | ||
|
||
|
||
def rotation_matrix(angle, axis): | ||
"""Generates a rotation matrix for a given angle and axis. | ||
Args: | ||
angle (float): Rotation angle in radians. | ||
axis (str): Axis for rotation: 'x', 'y', 'z' | ||
Returns: | ||
ndarray: Rotation matrix. | ||
Raises: | ||
ValueError: Invalid input axis. | ||
""" | ||
direction = np.zeros(3, dtype=float) | ||
if axis == 'x': | ||
direction[0] = 1 | ||
elif axis == 'y': | ||
direction[1] = 1 | ||
elif axis == 'z': | ||
direction[2] = 1 | ||
else: | ||
raise ValueError('Invalid axis.') | ||
direction = np.asarray(direction, dtype=float) | ||
sin_angle = math.sin(angle) | ||
cos_angle = math.cos(angle) | ||
rot = np.diag([cos_angle, cos_angle, cos_angle]) | ||
rot += np.outer(direction, direction) * (1.0 - cos_angle) | ||
direction *= sin_angle | ||
rot += np.array([ | ||
[0, -direction[2], direction[1]], | ||
[direction[2], 0, -direction[0]], | ||
[-direction[1], direction[0], 0] | ||
]) | ||
return rot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters