Skip to content

Commit

Permalink
Fix to_matrix test
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Feb 14, 2020
1 parent ea8361d commit 457d60b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
7 changes: 1 addition & 6 deletions test/python/circuit/test_controlled_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,7 @@ def test_controlled_standard_gates(self, num_ctrl_qubits):
# 'object has no attribute "control"'
# skipping Id and Barrier
continue
if gate.name == 'rz':
iden = Operator.from_label('I')
zgen = Operator.from_label('Z')
base_mat = (np.cos(0.5 * theta) * iden - 1j * np.sin(0.5 * theta) * zgen).data
else:
base_mat = Operator(gate).data
base_mat = Operator(gate).data
target_mat = _compute_control_matrix(base_mat, num_ctrl_qubits)
self.assertTrue(matrix_equal(Operator(cgate).data, target_mat, ignore_phase=True))

Expand Down
10 changes: 6 additions & 4 deletions test/python/circuit/test_extensions_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
from inspect import signature
from ddt import ddt, data, unpack

from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, execute
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile
from qiskit.qasm import pi
from qiskit.exceptions import QiskitError
from qiskit.circuit.exceptions import CircuitError
from qiskit.test import QiskitTestCase
from qiskit.circuit import Gate, ControlledGate, ParameterVector
from qiskit import BasicAer
from qiskit.quantum_info.operators import Operator
from qiskit.quantum_info.operators.predicates import matrix_equal, is_unitary_matrix

from qiskit.extensions.standard import (
Expand Down Expand Up @@ -1354,7 +1354,6 @@ def test_to_matrix(self):
definition."""
params = [0.1 * i for i in range(10)]
gate_class_list = Gate.__subclasses__() + ControlledGate.__subclasses__()
simulator = BasicAer.get_backend('unitary_simulator')
for gate_class in gate_class_list:
sig = signature(gate_class.__init__)
free_params = len(sig.parameters) - 1 # subtract "self"
Expand All @@ -1376,7 +1375,10 @@ def test_to_matrix(self):
self.log.info('to_matrix method FAILED for "%s" gate',
gate.name)
continue
definition_unitary = execute([circ], simulator).result().get_unitary()
# Unroll gate definition
ucirc = transpile(circ, optimization_level=0, basis_gates=['id', 'u3', 'cx'])
# Unrolled Unitary
definition_unitary = Operator(ucirc).data
self.assertTrue(matrix_equal(definition_unitary, gate_matrix))
self.assertTrue(is_unitary_matrix(gate_matrix))

Expand Down

0 comments on commit 457d60b

Please sign in to comment.