Skip to content

Commit

Permalink
fix statevector handling global phase (#5239)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
ajavadia and mtreinish authored Oct 15, 2020
1 parent 2dbe776 commit cf0b039
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/quantum_info/states/statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ def _evolve_instruction(statevec, obj, qargs=None):
raise QiskitError('{} instruction definition is {}; expected QuantumCircuit'.format(
obj.name, type(obj.definition)))
if obj.definition.global_phase:
statevec._data *= np.exp(1j * obj.definition.global_phase)
statevec._data *= np.exp(1j * float(obj.definition.global_phase))
for instr, qregs, cregs in obj.definition:
if cregs:
raise QiskitError(
Expand Down
12 changes: 12 additions & 0 deletions test/python/quantum_info/states/test_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qiskit.test import QiskitTestCase
from qiskit import QiskitError
from qiskit import QuantumRegister, QuantumCircuit
from qiskit import transpile
from qiskit.circuit.library import HGate

from qiskit.quantum_info.random import random_unitary
Expand Down Expand Up @@ -866,6 +867,17 @@ def test_expval(self):
expval = psi.expectation_value(op)
self.assertAlmostEqual(expval, target)

def test_global_phase(self):
"""Test global phase is handled correctly when evolving statevector."""

qc = QuantumCircuit(1)
qc.rz(0.5, 0)
qc2 = transpile(qc, basis_gates=['p'])
sv = Statevector.from_instruction(qc2)
expected = np.array([0.96891242-0.24740396j, 0])
self.assertEqual(float(qc2.global_phase), -1/4)
self.assertEqual(sv, Statevector(expected))


if __name__ == '__main__':
unittest.main()

0 comments on commit cf0b039

Please sign in to comment.