Skip to content

Commit

Permalink
Use Operator rather than unitary simulator to convert circuit to unit…
Browse files Browse the repository at this point in the history
…ary 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.
  • Loading branch information
jlapeyre authored Sep 12, 2020
1 parent 5551c43 commit 436ba5f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
12 changes: 3 additions & 9 deletions qiskit/aqua/operators/primitive_ops/circuit_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import logging
import numpy as np

from qiskit import QuantumCircuit, BasicAer, execute
import qiskit
from qiskit import QuantumCircuit
from qiskit.circuit.library import IGate
from qiskit.circuit import Instruction, ParameterExpression

Expand Down Expand Up @@ -139,14 +140,7 @@ def to_matrix(self, massive: bool = False) -> np.ndarray:
' in this case {0}x{0} elements.'
' Set massive=True if you want to proceed.'.format(2 ** self.num_qubits))

# NOTE: not reversing qubits!! We generally reverse endianness when converting between
# circuit or Pauli representation and matrix representation, but we don't need to here
# because the Unitary simulator already presents the endianness of the circuit unitary in
# forward endianness.
unitary_backend = BasicAer.get_backend('unitary_simulator')
unitary = execute(self.to_circuit(),
unitary_backend,
optimization_level=0).result().get_unitary()
unitary = qiskit.quantum_info.Operator(self.to_circuit()).data
# pylint: disable=cyclic-import
from ..operator_globals import EVAL_SIG_DIGITS
return np.round(unitary * self.coeff, decimals=EVAL_SIG_DIGITS)
Expand Down
6 changes: 3 additions & 3 deletions qiskit/aqua/operators/primitive_ops/pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ def exp_i(self) -> OperatorBase:
else self.coeff
# Y rotation
if corrected_x[sig_qubit_index] and corrected_z[sig_qubit_index]:
rot_op = PrimitiveOp(RYGate(coeff))
rot_op = PrimitiveOp(RYGate(2 * coeff))
# Z rotation
elif corrected_z[sig_qubit_index]:
rot_op = PrimitiveOp(RZGate(coeff))
rot_op = PrimitiveOp(RZGate(2 * coeff))
# X rotation
elif corrected_x[sig_qubit_index]:
rot_op = PrimitiveOp(RXGate(coeff))
rot_op = PrimitiveOp(RXGate(2 * coeff))

from ..operator_globals import I
left_pad = I.tensorpower(sig_qubit_index)
Expand Down

0 comments on commit 436ba5f

Please sign in to comment.