Skip to content

Commit

Permalink
Moving quaternions (Qiskit#1383)
Browse files Browse the repository at this point in the history
* Moving quaternions around

* Linting

* Update unitary.py

* Update qiskit/quantum_info/operators/unitary.py

Co-Authored-By: jaygambetta <[email protected]>
  • Loading branch information
jaygambetta authored and ajavadia committed Nov 30, 2018
1 parent fed597d commit 7b5b99a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion qiskit/mapper/_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from qiskit.dagcircuit._dagcircuiterror import DAGCircuitError
from qiskit.unrollers._dagunroller import DagUnroller
from qiskit.unrollers._dagbackend import DAGBackend
from qiskit.mapper._quaternion import quaternion_from_euler
from qiskit.quantum_info.operators.quaternion import quaternion_from_euler
from qiskit import QuantumRegister

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,39 +144,3 @@ def quaternion_from_euler(angles, order='yzy'):
* quaternion_from_axis_rotation(angles[2], order[2]))
quat.normalize(inplace=True)
return quat


def _rotm(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
48 changes: 48 additions & 0 deletions qiskit/quantum_info/operators/unitary.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"""Tests qiskit/mapper/_quaternion"""
import numpy as np
import scipy.linalg as la
from qiskit.mapper._quaternion import (quaternion_from_euler, _rotm)
from .common import QiskitTestCase
from qiskit.quantum_info.operators.quaternion import quaternion_from_euler
from qiskit.quantum_info.operators.unitary import rotation_matrix
from ..common import QiskitTestCase


class TestQuaternions(QiskitTestCase):
Expand All @@ -22,8 +23,9 @@ def test_random_euler(self):
for _ in range(1000):
rnd = 4*np.pi*(np.random.random(3)-0.5)
idx = np.random.randint(3, size=3)
mat1 = _rotm(rnd[0], axes[idx[0]]).dot(
_rotm(rnd[1], axes[idx[1]]).dot(_rotm(rnd[2], axes[idx[2]])))
mat1 = rotation_matrix(rnd[0], axes[idx[0]]).dot(
rotation_matrix(rnd[1], axes[idx[1]]).dot(
rotation_matrix(rnd[2], axes[idx[2]])))
axes_str = ''.join(axes[i] for i in idx)
quat = quaternion_from_euler(rnd, axes_str)
mat2 = quat.to_matrix()
Expand Down

0 comments on commit 7b5b99a

Please sign in to comment.