Skip to content

Commit ca4bf30

Browse files
authored
Use Operator rather than unitary simulator to convert circuit to unitary matrix (qiskit-community/qiskit-aqua#1224)
* Fix factor of 2 error in converting exp_i to rotation gates * Make CircuitOp use Operator rather than unitary simulator to get matrix The unitary simulator does not account for the global phase in a circuit. qiskit.quantum_info.Operator does account for global phase when converting (when possible) a circuit to a unitary matrix. Closes qiskit-community/qiskit-aqua#1218 * Remove note on reversing order of qubits Using Operator(QuantumCircuit) gives the same qubit ordering that the previous unitary-simulator-based code did. I removed the comment entirely because the behavior depends only on Operator(QuantumCircuit), which is not obscure.
1 parent 7580c0f commit ca4bf30

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

test/aqua/operators/test_evolution.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import scipy.linalg
2020

21+
import qiskit
2122
from qiskit.circuit import ParameterVector, Parameter
2223

2324
from qiskit.aqua.operators import (X, Y, Z, I, CX, H, ListOp, CircuitOp, Zero, EvolutionFactory,
@@ -29,6 +30,13 @@
2930
class TestEvolution(QiskitAquaTestCase):
3031
"""Evolution tests."""
3132

33+
def test_exp_i(self):
34+
""" exponential of Pauli test """
35+
op = Z.exp_i()
36+
gate = op.to_circuit().data[0][0]
37+
self.assertIsInstance(gate, qiskit.circuit.library.RZGate)
38+
self.assertEqual(gate.params[0], 2)
39+
3240
def test_pauli_evolution(self):
3341
""" pauli evolution test """
3442
op = (-1.052373245772859 * I ^ I) + \

test/aqua/operators/test_op_construction.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from test.aqua import QiskitAquaTestCase
1919
import itertools
20+
import scipy
2021
from scipy.stats import unitary_group
2122
import numpy as np
2223
from ddt import ddt, data
@@ -201,6 +202,14 @@ def test_to_matrix(self):
201202
np.testing.assert_array_almost_equal(
202203
op6.to_matrix(), op5.to_matrix() + Operator.from_label('+r').data)
203204

205+
def test_circuit_op_to_matrix(self):
206+
""" test CircuitOp.to_matrix """
207+
qc = QuantumCircuit(1)
208+
qc.rz(1.0, 0)
209+
qcop = CircuitOp(qc)
210+
np.testing.assert_array_almost_equal(
211+
qcop.to_matrix(), scipy.linalg.expm(-0.5j * Z.to_matrix()))
212+
204213
def test_matrix_to_instruction(self):
205214
"""Test MatrixOp.to_instruction yields an Instruction object."""
206215
matop = (H ^ 3).to_matrix_op()

0 commit comments

Comments
 (0)