replacing self.assertTrue(numpy.allclose()) by numpy.assert_allclose in tests (#2924)

* from unittest.mock import patch

* .

* shorter test/python/test_dagcircuit.py

* shorter test/python/test_dagcircuit.py

* test.python.quantum_info.test_weyl

* test.python.quantum_info.states.test_statevector

* test/python/quantum_info/states/test_densitymatrix.py

* test.python.quantum_info.test_quaternions

* test.python.quantum_info.test_local_invariance
/

* test.python.circuit.test_unitary

* test.python.quantum_info.operators.test_operator

* test.python.quantum_info.operators.channel.test_chi

* test/python/quantum_info/operators/channel/test_superop.py

* test/python/quantum_info/operators/channel/test_choi.py

* test/python/quantum_info/operators/channel/test_evolve.py

* test/python/quantum_info/operators/channel

* lint

* unused import

* atol TestWeyl
This commit is contained in:
Luciano 2019-08-08 18:45:56 -04:00 committed by GitHub
parent 3517e6cf38
commit 51992588b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 99 additions and 135 deletions

View File

@ -16,6 +16,7 @@
import json
import numpy
from numpy.testing import assert_allclose
import qiskit
from qiskit.extensions.unitary import UnitaryGate
@ -95,7 +96,7 @@ class TestUnitaryCircuit(QiskitTestCase):
self.assertIsInstance(dnode.op, UnitaryGate)
for qubit in dnode.qargs:
self.assertTrue(qubit.index in [0, 1])
self.assertTrue(numpy.allclose(dnode.op.to_matrix(), matrix))
assert_allclose(dnode.op.to_matrix(), matrix)
def test_2q_unitary(self):
"""test 2 qubit unitary matrix"""
@ -123,8 +124,7 @@ class TestUnitaryCircuit(QiskitTestCase):
self.assertIsInstance(dnode.op, UnitaryGate)
for qubit in dnode.qargs:
self.assertTrue(qubit.index in [0, 1])
self.assertTrue(numpy.allclose(dnode.op.to_matrix(),
matrix))
assert_allclose(dnode.op.to_matrix(), matrix)
qc3 = dag_to_circuit(dag)
self.assertEqual(qc2, qc3)
@ -148,8 +148,7 @@ class TestUnitaryCircuit(QiskitTestCase):
self.assertIsInstance(dnode.op, UnitaryGate)
for qubit in dnode.qargs:
self.assertTrue(qubit.index in [0, 1, 3])
self.assertTrue(numpy.allclose(dnode.op.to_matrix(),
matrix))
assert_allclose(dnode.op.to_matrix(), matrix)
def test_qobj_with_unitary_matrix(self):
"""test qobj output with unitary matrix"""
@ -165,9 +164,7 @@ class TestUnitaryCircuit(QiskitTestCase):
qobj = qiskit.compiler.assemble(qc)
instr = qobj.experiments[0].instructions[1]
self.assertEqual(instr.name, 'unitary')
self.assertTrue(numpy.allclose(
numpy.array(instr.params).astype(numpy.complex64),
matrix))
assert_allclose(numpy.array(instr.params[0]).astype(numpy.complex64), matrix)
# check conversion to dict
qobj_dict = qobj.to_dict()
# check json serialization

View File

@ -16,6 +16,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError
from qiskit.quantum_info.states import DensityMatrix
@ -30,12 +31,12 @@ class TestChi(ChannelTestCase):
"""Test initialization"""
mat4 = np.eye(4) / 2.0
chan = Chi(mat4)
self.assertAllClose(chan.data, mat4)
assert_allclose(chan.data, mat4)
self.assertEqual(chan.dim, (2, 2))
mat16 = np.eye(16) / 4
chan = Chi(mat16)
self.assertAllClose(chan.data, mat16)
assert_allclose(chan.data, mat16)
self.assertEqual(chan.dim, (4, 4))
# Wrong input or output dims should raise exception

View File

@ -18,6 +18,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError
from qiskit.quantum_info.states import DensityMatrix
@ -32,21 +33,21 @@ class TestChoi(ChannelTestCase):
"""Test initialization"""
mat4 = np.eye(4) / 2.0
chan = Choi(mat4)
self.assertAllClose(chan.data, mat4)
assert_allclose(chan.data, mat4)
self.assertEqual(chan.dim, (2, 2))
mat8 = np.eye(8) / 2.0
chan = Choi(mat8, input_dims=4)
self.assertAllClose(chan.data, mat8)
assert_allclose(chan.data, mat8)
self.assertEqual(chan.dim, (4, 2))
chan = Choi(mat8, input_dims=2)
self.assertAllClose(chan.data, mat8)
assert_allclose(chan.data, mat8)
self.assertEqual(chan.dim, (2, 4))
mat16 = np.eye(16) / 4
chan = Choi(mat16)
self.assertAllClose(chan.data, mat16)
assert_allclose(chan.data, mat16)
self.assertEqual(chan.dim, (4, 4))
# Wrong input or output dims should raise exception

View File

@ -17,6 +17,7 @@
"""Tests for quantum channel representation transformations."""
import unittest
from numpy.testing import assert_allclose
from qiskit.quantum_info.states.densitymatrix import DensityMatrix
from qiskit.quantum_info.operators.operator import Operator
@ -45,7 +46,7 @@ class TestEvolve(ChannelTestCase):
chan = Operator(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _other_to_operator(self, rep, qubits_test_cases, repetitions):
"""Test Other to Operator evolution."""
@ -57,7 +58,7 @@ class TestEvolve(ChannelTestCase):
chan = rep(Operator(mat))
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(Operator(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _choi_to_other_cp(self, rep, qubits_test_cases, repetitions):
"""Test CP Choi to Other evolution."""
@ -69,7 +70,7 @@ class TestEvolve(ChannelTestCase):
chan = Choi(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _choi_to_other_noncp(self, rep, qubits_test_cases, repetitions):
"""Test CP Choi to Other evolution."""
@ -81,7 +82,7 @@ class TestEvolve(ChannelTestCase):
chan = Choi(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _superop_to_other(self, rep, qubits_test_cases, repetitions):
"""Test SuperOp to Other evolution."""
@ -93,7 +94,7 @@ class TestEvolve(ChannelTestCase):
chan = SuperOp(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _kraus_to_other_single(self, rep, qubits_test_cases, repetitions):
"""Test single Kraus to Other evolution."""
@ -105,7 +106,7 @@ class TestEvolve(ChannelTestCase):
chan = Kraus(kraus)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _kraus_to_other_double(self, rep, qubits_test_cases, repetitions):
"""Test double Kraus to Other evolution."""
@ -118,7 +119,7 @@ class TestEvolve(ChannelTestCase):
chan = Kraus((kraus_l, kraus_r))
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _stinespring_to_other_single(self, rep, qubits_test_cases,
repetitions):
@ -131,7 +132,7 @@ class TestEvolve(ChannelTestCase):
chan = Stinespring(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _stinespring_to_other_double(self, rep, qubits_test_cases,
repetitions):
@ -145,7 +146,7 @@ class TestEvolve(ChannelTestCase):
chan = Stinespring((mat_l, mat_r))
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _chi_to_other(self, rep, qubits_test_cases, repetitions):
"""Test Chi to Other evolution."""
@ -157,7 +158,7 @@ class TestEvolve(ChannelTestCase):
chan = Chi(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def _ptm_to_other(self, rep, qubits_test_cases, repetitions):
"""Test PTM to Other evolution."""
@ -169,7 +170,7 @@ class TestEvolve(ChannelTestCase):
chan = PTM(mat)
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(rep(chan)).data
self.assertAllClose(rho1, rho2)
assert_allclose(rho1, rho2)
def test_unitary_to_choi(self):
"""Test Operator to Choi evolution."""

View File

@ -16,6 +16,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError
from qiskit.quantum_info.states import DensityMatrix
@ -30,29 +31,29 @@ class TestKraus(ChannelTestCase):
"""Test initialization"""
# Initialize from unitary
chan = Kraus(self.UI)
self.assertAllClose(chan.data, [self.UI])
assert_allclose(chan.data, [self.UI])
self.assertEqual(chan.dim, (2, 2))
# Initialize from Kraus
chan = Kraus(self.depol_kraus(0.5))
self.assertAllClose(chan.data, self.depol_kraus(0.5))
assert_allclose(chan.data, self.depol_kraus(0.5))
self.assertEqual(chan.dim, (2, 2))
# Initialize from Non-CPTP
kraus_l, kraus_r = [self.UI, self.UX], [self.UY, self.UZ]
chan = Kraus((kraus_l, kraus_r))
self.assertAllClose(chan.data, (kraus_l, kraus_r))
assert_allclose(chan.data, (kraus_l, kraus_r))
self.assertEqual(chan.dim, (2, 2))
# Initialize with redundant second op
chan = Kraus((kraus_l, kraus_l))
self.assertAllClose(chan.data, kraus_l)
assert_allclose(chan.data, kraus_l)
self.assertEqual(chan.dim, (2, 2))
# Initialize from rectangular
kraus = [np.zeros((4, 2))]
chan = Kraus(kraus)
self.assertAllClose(chan.data, kraus)
assert_allclose(chan.data, kraus)
self.assertEqual(chan.dim, (2, 4))
# Wrong input or output dims should raise exception

View File

@ -16,6 +16,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError
from qiskit.quantum_info.states import DensityMatrix
@ -30,12 +31,12 @@ class TestPTM(ChannelTestCase):
"""Test initialization"""
mat4 = np.eye(4) / 2.0
chan = PTM(mat4)
self.assertAllClose(chan.data, mat4)
assert_allclose(chan.data, mat4)
self.assertEqual(chan.dim, (2, 2))
mat16 = np.eye(16) / 4
chan = PTM(mat16)
self.assertAllClose(chan.data, mat16)
assert_allclose(chan.data, mat16)
self.assertEqual(chan.dim, (4, 4))
# Wrong input or output dims should raise exception

View File

@ -16,6 +16,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError
from qiskit.quantum_info.states import DensityMatrix
@ -30,23 +31,23 @@ class TestStinespring(ChannelTestCase):
"""Test initialization"""
# Initialize from unitary
chan = Stinespring(self.UI)
self.assertAllClose(chan.data, self.UI)
assert_allclose(chan.data, self.UI)
self.assertEqual(chan.dim, (2, 2))
# Initialize from Stinespring
chan = Stinespring(self.depol_stine(0.5))
self.assertAllClose(chan.data, self.depol_stine(0.5))
assert_allclose(chan.data, self.depol_stine(0.5))
self.assertEqual(chan.dim, (2, 2))
# Initialize from Non-CPTP
stine_l, stine_r = self.rand_matrix(4, 2), self.rand_matrix(4, 2)
chan = Stinespring((stine_l, stine_r))
self.assertAllClose(chan.data, (stine_l, stine_r))
assert_allclose(chan.data, (stine_l, stine_r))
self.assertEqual(chan.dim, (2, 2))
# Initialize with redundant second op
chan = Stinespring((stine_l, stine_l))
self.assertAllClose(chan.data, stine_l)
assert_allclose(chan.data, stine_l)
self.assertEqual(chan.dim, (2, 2))
# Wrong input or output dims should raise exception

View File

@ -16,6 +16,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit import QiskitError, QuantumCircuit
from qiskit.quantum_info.states import DensityMatrix
@ -30,16 +31,16 @@ class TestSuperOp(ChannelTestCase):
def test_init(self):
"""Test initialization"""
chan = SuperOp(self.sopI)
self.assertAllClose(chan.data, self.sopI)
assert_allclose(chan.data, self.sopI)
self.assertEqual(chan.dim, (2, 2))
mat = np.zeros((4, 16))
chan = SuperOp(mat)
self.assertAllClose(chan.data, mat)
assert_allclose(chan.data, mat)
self.assertEqual(chan.dim, (4, 2))
chan = SuperOp(mat.T)
self.assertAllClose(chan.data, mat.T)
assert_allclose(chan.data, mat.T)
self.assertEqual(chan.dim, (2, 4))
# Wrong input or output dims should raise exception

View File

@ -19,6 +19,7 @@
import unittest
import logging
import numpy as np
from numpy.testing import assert_allclose
import scipy.linalg as la
from qiskit import QiskitError
@ -87,21 +88,6 @@ class OperatorTestCase(QiskitTestCase):
circ.measure(qr, cr)
return circ
def assertAllClose(self,
obj1,
obj2,
rtol=1e-5,
atol=1e-6,
equal_nan=False,
msg=None):
"""Assert two objects are equal using Numpy.allclose."""
comparison = np.allclose(
obj1, obj2, rtol=rtol, atol=atol, equal_nan=equal_nan)
if msg is None:
msg = ''
msg += '({} != {})'.format(obj1, obj2)
self.assertTrue(comparison, msg=msg)
class TestOperator(OperatorTestCase):
"""Tests for Operator linear operator class."""
@ -111,13 +97,13 @@ class TestOperator(OperatorTestCase):
# Test automatic inference of qubit subsystems
mat = self.rand_matrix(8, 8)
op = Operator(mat)
self.assertAllClose(op.data, mat)
assert_allclose(op.data, mat)
self.assertEqual(op.dim, (8, 8))
self.assertEqual(op.input_dims(), (2, 2, 2))
self.assertEqual(op.output_dims(), (2, 2, 2))
op = Operator(mat, input_dims=8, output_dims=8)
self.assertAllClose(op.data, mat)
assert_allclose(op.data, mat)
self.assertEqual(op.dim, (8, 8))
self.assertEqual(op.input_dims(), (2, 2, 2))
self.assertEqual(op.output_dims(), (2, 2, 2))
@ -126,14 +112,14 @@ class TestOperator(OperatorTestCase):
"""Test initialization from array."""
mat = np.eye(3)
op = Operator(mat)
self.assertAllClose(op.data, mat)
assert_allclose(op.data, mat)
self.assertEqual(op.dim, (3, 3))
self.assertEqual(op.input_dims(), (3,))
self.assertEqual(op.output_dims(), (3,))
mat = self.rand_matrix(2 * 3 * 4, 4 * 5)
op = Operator(mat, input_dims=[4, 5], output_dims=[2, 3, 4])
self.assertAllClose(op.data, mat)
assert_allclose(op.data, mat)
self.assertEqual(op.dim, (4 * 5, 2 * 3 * 4))
self.assertEqual(op.input_dims(), (4, 5))
self.assertEqual(op.output_dims(), (2, 3, 4))
@ -224,7 +210,7 @@ class TestOperator(OperatorTestCase):
"""Test Operator representation string property."""
mat = self.rand_matrix(2, 2)
op = Operator(mat)
self.assertAllClose(mat, op.data)
assert_allclose(mat, op.data)
def test_dim(self):
"""Test Operator dim property."""
@ -439,12 +425,12 @@ class TestOperator(OperatorTestCase):
mat21 = np.kron(mat2, mat1)
op21 = Operator(mat1).expand(Operator(mat2))
self.assertEqual(op21.dim, (6, 6))
self.assertAllClose(op21.data, Operator(mat21).data)
assert_allclose(op21.data, Operator(mat21).data)
mat12 = np.kron(mat1, mat2)
op12 = Operator(mat2).expand(Operator(mat1))
self.assertEqual(op12.dim, (6, 6))
self.assertAllClose(op12.data, Operator(mat12).data)
assert_allclose(op12.data, Operator(mat12).data)
def test_tensor(self):
"""Test tensor method."""
@ -454,12 +440,12 @@ class TestOperator(OperatorTestCase):
mat21 = np.kron(mat2, mat1)
op21 = Operator(mat2).tensor(Operator(mat1))
self.assertEqual(op21.dim, (6, 6))
self.assertAllClose(op21.data, Operator(mat21).data)
assert_allclose(op21.data, Operator(mat21).data)
mat12 = np.kron(mat1, mat2)
op12 = Operator(mat1).tensor(Operator(mat2))
self.assertEqual(op12.dim, (6, 6))
self.assertAllClose(op12.data, Operator(mat12).data)
assert_allclose(op12.data, Operator(mat12).data)
def test_power_except(self):
"""Test power method raises exceptions."""

View File

@ -20,6 +20,7 @@ import unittest
import logging
import numpy as np
from numpy.testing import assert_allclose
from qiskit.test import QiskitTestCase
from qiskit import QiskitError
@ -53,28 +54,13 @@ class TestDensityMatrix(QiskitTestCase):
rho = cls.rand_vec(n, normalize=True)
return np.outer(rho, np.conjugate(rho))
def assertAllClose(self,
obj1,
obj2,
rtol=1e-5,
atol=1e-6,
equal_nan=False,
msg=None):
"""Assert two objects are equal using Numpy.allclose."""
comparison = np.allclose(
obj1, obj2, rtol=rtol, atol=atol, equal_nan=equal_nan)
if msg is None:
msg = ''
msg += '({} != {})'.format(obj1, obj2)
self.assertTrue(comparison, msg=msg)
def test_init_array_qubit(self):
"""Test subsystem initialization from N-qubit array."""
# Test automatic inference of qubit subsystems
rho = self.rand_rho(8)
for dims in [None, 8]:
state = DensityMatrix(rho, dims=dims)
self.assertAllClose(state.data, rho)
assert_allclose(state.data, rho)
self.assertEqual(state.dim, 8)
self.assertEqual(state.dims(), (2, 2, 2))
@ -82,13 +68,13 @@ class TestDensityMatrix(QiskitTestCase):
"""Test initialization from array."""
rho = self.rand_rho(3)
state = DensityMatrix(rho)
self.assertAllClose(state.data, rho)
assert_allclose(state.data, rho)
self.assertEqual(state.dim, 3)
self.assertEqual(state.dims(), (3,))
rho = self.rand_rho(2 * 3 * 4)
state = DensityMatrix(rho, dims=[2, 3, 4])
self.assertAllClose(state.data, rho)
assert_allclose(state.data, rho)
self.assertEqual(state.dim, 2 * 3 * 4)
self.assertEqual(state.dims(), (2, 3, 4))
@ -293,7 +279,7 @@ class TestDensityMatrix(QiskitTestCase):
state = DensityMatrix(rho0).expand(DensityMatrix(rho1))
self.assertEqual(state.dim, 6)
self.assertEqual(state.dims(), (2, 3))
self.assertAllClose(state.data, target)
assert_allclose(state.data, target)
def test_tensor(self):
"""Test tensor method."""
@ -304,7 +290,7 @@ class TestDensityMatrix(QiskitTestCase):
state = DensityMatrix(rho0).tensor(DensityMatrix(rho1))
self.assertEqual(state.dim, 6)
self.assertEqual(state.dims(), (3, 2))
self.assertAllClose(state.data, target)
assert_allclose(state.data, target)
def test_add(self):
"""Test add method."""

View File

@ -19,6 +19,7 @@
import unittest
import logging
import numpy as np
from numpy.testing import assert_allclose
from qiskit.test import QiskitTestCase
from qiskit import QiskitError
@ -48,28 +49,13 @@ class TestStatevector(QiskitTestCase):
vec /= np.sqrt(np.dot(vec, np.conj(vec)))
return vec
def assertAllClose(self,
obj1,
obj2,
rtol=1e-5,
atol=1e-6,
equal_nan=False,
msg=None):
"""Assert two objects are equal using Numpy.allclose."""
comparison = np.allclose(
obj1, obj2, rtol=rtol, atol=atol, equal_nan=equal_nan)
if msg is None:
msg = ''
msg += '({} != {})'.format(obj1, obj2)
self.assertTrue(comparison, msg=msg)
def test_init_array_qubit(self):
"""Test subsystem initialization from N-qubit array."""
# Test automatic inference of qubit subsystems
vec = self.rand_vec(8)
for dims in [None, 8]:
state = Statevector(vec, dims=dims)
self.assertAllClose(state.data, vec)
assert_allclose(state.data, vec)
self.assertEqual(state.dim, 8)
self.assertEqual(state.dims(), (2, 2, 2))
@ -77,13 +63,13 @@ class TestStatevector(QiskitTestCase):
"""Test initialization from array."""
vec = self.rand_vec(3)
statevec = Statevector(vec)
self.assertAllClose(statevec.data, vec)
assert_allclose(statevec.data, vec)
self.assertEqual(statevec.dim, 3)
self.assertEqual(statevec.dims(), (3,))
vec = self.rand_vec(2 * 3 * 4)
state = Statevector(vec, dims=[2, 3, 4])
self.assertAllClose(state.data, vec)
assert_allclose(state.data, vec)
self.assertEqual(state.dim, 2 * 3 * 4)
self.assertEqual(state.dims(), (2, 3, 4))
@ -284,7 +270,7 @@ class TestStatevector(QiskitTestCase):
state = Statevector(vec0).expand(Statevector(vec1))
self.assertEqual(state.dim, 6)
self.assertEqual(state.dims(), (2, 3))
self.assertAllClose(state.data, target)
assert_allclose(state.data, target)
def test_tensor(self):
"""Test tensor method."""
@ -295,7 +281,7 @@ class TestStatevector(QiskitTestCase):
state = Statevector(vec0).tensor(Statevector(vec1))
self.assertEqual(state.dim, 6)
self.assertEqual(state.dims(), (3, 2))
self.assertAllClose(state.data, target)
assert_allclose(state.data, target)
def test_add(self):
"""Test add method."""

View File

@ -17,7 +17,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit.execute import execute
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.test import QiskitTestCase
@ -38,14 +38,14 @@ class TestLocalInvariance(QiskitTestCase):
qc = QuantumCircuit(qr)
U = execute(qc, sim).result().get_unitary()
vec = two_qubit_local_invariants(U)
self.assertTrue(np.allclose(vec, [1, 0, 3]))
assert_allclose(vec, [1, 0, 3])
qr = QuantumRegister(2, name='q')
qc = QuantumCircuit(qr)
qc.cx(qr[1], qr[0])
U = execute(qc, sim).result().get_unitary()
vec = two_qubit_local_invariants(U)
self.assertTrue(np.allclose(vec, [0, 0, 1]))
assert_allclose(vec, [0, 0, 1])
qr = QuantumRegister(2, name='q')
qc = QuantumCircuit(qr)
@ -53,14 +53,14 @@ class TestLocalInvariance(QiskitTestCase):
qc.cx(qr[0], qr[1])
U = execute(qc, sim).result().get_unitary()
vec = two_qubit_local_invariants(U)
self.assertTrue(np.allclose(vec, [0, 0, -1]))
assert_allclose(vec, [0, 0, -1])
qr = QuantumRegister(2, name='q')
qc = QuantumCircuit(qr)
qc.swap(qr[1], qr[0])
U = execute(qc, sim).result().get_unitary()
vec = two_qubit_local_invariants(U)
self.assertTrue(np.allclose(vec, [-1, 0, -3]))
assert_allclose(vec, [-1, 0, -3])
if __name__ == '__main__':

View File

@ -16,6 +16,7 @@
import math
import numpy as np
from numpy.testing import assert_allclose
import scipy.linalg as la
from qiskit.quantum_info.operators.quaternion import \
@ -59,15 +60,15 @@ class TestQuaternions(QiskitTestCase):
def test_random_euler(self):
"""Quaternion from Euler rotations."""
self.assertTrue(np.allclose(self.mat1, self.mat2))
assert_allclose(self.mat1, self.mat2)
def test_orthogonality(self):
"""Quaternion rotation matrix orthogonality"""
self.assertTrue(np.allclose(self.mat2.dot(self.mat2.T), np.identity(3, dtype=float)))
assert_allclose(self.mat2.dot(self.mat2.T), np.identity(3, dtype=float), atol=1e-8)
def test_det(self):
"""Quaternion det = 1"""
self.assertTrue(np.allclose(la.det(self.mat2), 1))
assert_allclose(la.det(self.mat2), 1)
def test_equiv_quaternions(self):
"""Different Euler rotations give same quaternion, up to sign."""
@ -79,7 +80,7 @@ class TestQuaternions(QiskitTestCase):
quat1 = quaternion_from_euler(rnd, value)
euler = quat1.to_zyz()
quat2 = quaternion_from_euler(euler, 'zyz')
self.assertTrue(np.allclose(abs(quat1.data.dot(quat2.data)), 1))
assert_allclose(abs(quat1.data.dot(quat2.data)), 1)
def test_mul_by_quat(self):
"""Quarternions should multiply correctly."""
@ -89,7 +90,7 @@ class TestQuaternions(QiskitTestCase):
other_mat = other_quat.to_matrix()
product_quat = self.quat_unnormalized * other_quat
product_mat = (self.quat_unnormalized.to_matrix()).dot(other_mat)
self.assertTrue(np.allclose(product_quat.to_matrix(), product_mat))
assert_allclose(product_quat.to_matrix(), product_mat)
def test_mul_by_array(self):
"""Quaternions cannot be multiplied with an array."""
@ -104,14 +105,14 @@ class TestQuaternions(QiskitTestCase):
def test_rotation(self):
"""Multiplication by -1 should give the same rotation."""
neg_quat = Quaternion(self.quat_unnormalized.data * -1)
self.assertTrue(np.allclose(neg_quat.to_matrix(), self.quat_unnormalized.to_matrix()))
assert_allclose(neg_quat.to_matrix(), self.quat_unnormalized.to_matrix())
def test_one_euler_angle(self):
"""Quaternion should return a correct sequence of zyz representation
in the case of rotations when there is only one non-zero Euler angle."""
rand_rot_angle = 0.123456789
some_quat = quaternion_from_axis_rotation(rand_rot_angle, "z")
self.assertTrue(np.allclose(some_quat.to_zyz(), np.array([rand_rot_angle, 0, 0])))
assert_allclose(some_quat.to_zyz(), np.array([rand_rot_angle, 0, 0]))
def test_two_euler_angle_0123456789(self):
"""Quaternion should return a correct sequence of zyz representation
@ -120,7 +121,7 @@ class TestQuaternions(QiskitTestCase):
rand_rot_angle = 0.123456789
some_quat = (quaternion_from_axis_rotation(rand_rot_angle, "z")
* quaternion_from_axis_rotation(np.pi, "y"))
self.assertTrue(np.allclose(some_quat.to_zyz(), np.array([rand_rot_angle, np.pi, 0])))
assert_allclose(some_quat.to_zyz(), np.array([rand_rot_angle, np.pi, 0]))
def test_two_euler_angle_0987654321(self):
"""Quaternion should return a correct sequence of zyz representation
@ -129,7 +130,7 @@ class TestQuaternions(QiskitTestCase):
rand_rot_angle = 0.987654321
some_quat = (quaternion_from_axis_rotation(rand_rot_angle, "z")
* quaternion_from_axis_rotation(np.pi, "y"))
self.assertTrue(np.allclose(some_quat.to_zyz(), np.array([rand_rot_angle, np.pi, 0])))
assert_allclose(some_quat.to_zyz(), np.array([rand_rot_angle, np.pi, 0]))
def test_quaternion_from_rotation_invalid_axis(self):
"""Cannot generate quaternion from rotations around invalid axis."""

View File

@ -16,8 +16,9 @@
"""Tests for Weyl coorindate routines."""
import unittest
import numpy as np
from numpy.testing import assert_allclose
from qiskit.test import QiskitTestCase
from qiskit.quantum_info.random import random_unitary
from qiskit.quantum_info.synthesis.weyl import weyl_coordinates
@ -34,7 +35,7 @@ class TestWeyl(QiskitTestCase):
# Identity [0,0,0]
U = np.identity(4)
weyl = weyl_coordinates(U)
self.assertTrue(np.allclose(weyl, [0, 0, 0]))
assert_allclose(weyl, [0, 0, 0])
# CNOT [pi/4, 0, 0]
U = np.array([[1, 0, 0, 0],
@ -42,7 +43,7 @@ class TestWeyl(QiskitTestCase):
[0, 0, 1, 0],
[0, 1, 0, 0]], dtype=complex)
weyl = weyl_coordinates(U)
self.assertTrue(np.allclose(weyl, [np.pi/4, 0, 0]))
assert_allclose(weyl, [np.pi / 4, 0, 0], atol=1e-07)
# SWAP [pi/4, pi/4 ,pi/4]
U = np.array([[1, 0, 0, 0],
@ -51,16 +52,16 @@ class TestWeyl(QiskitTestCase):
[0, 0, 0, 1]], dtype=complex)
weyl = weyl_coordinates(U)
self.assertTrue(np.allclose(weyl, [np.pi/4, np.pi/4, np.pi/4]))
assert_allclose(weyl, [np.pi / 4, np.pi / 4, np.pi / 4])
# SQRT ISWAP [pi/8, pi/8, 0]
U = np.array([[1, 0, 0, 0],
[0, 1/np.sqrt(2), 1j/np.sqrt(2), 0],
[0, 1j/np.sqrt(2), 1/np.sqrt(2), 0],
[0, 1 / np.sqrt(2), 1j / np.sqrt(2), 0],
[0, 1j / np.sqrt(2), 1 / np.sqrt(2), 0],
[0, 0, 0, 1]], dtype=complex)
weyl = weyl_coordinates(U)
self.assertTrue(np.allclose(weyl, [np.pi/8, np.pi/8, 0]))
assert_allclose(weyl, [np.pi / 8, np.pi / 8, 0])
def test_weyl_coordinates_random(self):
"""Randomly check Weyl coordinates with local invariants.
@ -70,7 +71,7 @@ class TestWeyl(QiskitTestCase):
weyl = weyl_coordinates(U)
local_equiv = local_equivalence(weyl)
local = two_qubit_local_invariants(U)
self.assertTrue(np.allclose(local, local_equiv))
assert_allclose(local, local_equiv)
if __name__ == '__main__':

View File

@ -369,12 +369,12 @@ class TestDagOperations(QiskitTestCase):
expected = [('qr[0]', []),
('qr[1]', []),
('cx', [(QuantumRegister(3, 'qr'), 0), (QuantumRegister(3, 'qr'), 1)]),
('h', [(QuantumRegister(3, 'qr'), 0)]),
('cx', [self.qubit0, self.qubit1]),
('h', [self.qubit0]),
('qr[2]', []),
('cx', [(QuantumRegister(3, 'qr'), 2), (QuantumRegister(3, 'qr'), 1)]),
('cx', [(QuantumRegister(3, 'qr'), 0), (QuantumRegister(3, 'qr'), 2)]),
('h', [(QuantumRegister(3, 'qr'), 2)]),
('cx', [self.qubit2, self.qubit1]),
('cx', [self.qubit0, self.qubit2]),
('h', [self.qubit2]),
('qr[0]', []),
('qr[1]', []),
('qr[2]', []),
@ -394,11 +394,11 @@ class TestDagOperations(QiskitTestCase):
named_nodes = self.dag.topological_op_nodes()
expected = [('cx', [(QuantumRegister(3, 'qr'), 0), (QuantumRegister(3, 'qr'), 1)]),
('h', [(QuantumRegister(3, 'qr'), 0)]),
('cx', [(QuantumRegister(3, 'qr'), 2), (QuantumRegister(3, 'qr'), 1)]),
('cx', [(QuantumRegister(3, 'qr'), 0), (QuantumRegister(3, 'qr'), 2)]),
('h', [(QuantumRegister(3, 'qr'), 2)])]
expected = [('cx', [self.qubit0, self.qubit1]),
('h', [self.qubit0]),
('cx', [self.qubit2, self.qubit1]),
('cx', [self.qubit0, self.qubit2]),
('h', [self.qubit2])]
self.assertEqual(expected, [(i.name, i.qargs) for i in named_nodes])
def test_dag_nodes_on_wire(self):