From 457d60b90a978b0004f028f575b47bf5cebd2132 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Thu, 13 Feb 2020 14:45:46 -0800 Subject: [PATCH] Fix to_matrix test --- test/python/circuit/test_controlled_gate.py | 7 +------ test/python/circuit/test_extensions_standard.py | 10 ++++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/test/python/circuit/test_controlled_gate.py b/test/python/circuit/test_controlled_gate.py index d0ad34219a07..d48862f6ef17 100644 --- a/test/python/circuit/test_controlled_gate.py +++ b/test/python/circuit/test_controlled_gate.py @@ -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)) diff --git a/test/python/circuit/test_extensions_standard.py b/test/python/circuit/test_extensions_standard.py index ecf2020d3c69..929b0de36320 100644 --- a/test/python/circuit/test_extensions_standard.py +++ b/test/python/circuit/test_extensions_standard.py @@ -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 ( @@ -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" @@ -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))