Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix global phase in initializer #6236

Merged
merged 6 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions qiskit/extensions/quantum_initializer/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from qiskit.circuit.library.standard_gates.h import HGate
from qiskit.circuit.library.standard_gates.s import SGate, SdgGate
from qiskit.circuit.library.standard_gates.ry import RYGate
from qiskit.circuit.library.standard_gates.u1 import U1Gate
from qiskit.circuit.library.standard_gates.rz import RZGate
from qiskit.circuit.reset import Reset
from qiskit.quantum_info import Statevector

Expand Down Expand Up @@ -198,13 +198,13 @@ def gates_to_uncompute(self):
add_last_cnot = False

if np.linalg.norm(phis) != 0:
rz_mult = self._multiplex(U1Gate, phis, last_cnot=add_last_cnot)
rz_mult = self._multiplex(RZGate, phis, last_cnot=add_last_cnot)
circuit.append(rz_mult.to_instruction(), q[i:self.num_qubits])

if np.linalg.norm(thetas) != 0:
ry_mult = self._multiplex(RYGate, thetas, last_cnot=add_last_cnot)
circuit.append(ry_mult.to_instruction().reverse_ops(), q[i:self.num_qubits])

circuit.global_phase -= np.angle(sum(remaining_param))
return circuit

@staticmethod
Expand Down
40 changes: 39 additions & 1 deletion test/python/circuit/test_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit import ClassicalRegister
from qiskit import transpile
from qiskit import execute, assemble, BasicAer
from qiskit.quantum_info import state_fidelity, Statevector
from qiskit.quantum_info import state_fidelity, Statevector, Operator
from qiskit.exceptions import QiskitError
from qiskit.test import QiskitTestCase

Expand Down Expand Up @@ -367,6 +367,44 @@ def test_from_int(self):
actual_sv = Statevector.from_instruction(qc)
self.assertTrue(desired_sv == actual_sv)

def _remove_resets(self, circ):
circ.data = [tup for tup in circ.data if tup[0].name != 'reset']

def test_global_phase_random(self):
ewinston marked this conversation as resolved.
Show resolved Hide resolved
"""Test global phase preservation with random state vectors"""
from qiskit.quantum_info.random import random_statevector
repeats = 5
for n_qubits in [1, 2, 4]:
for irep in range(repeats):
with self.subTest(i=f'{n_qubits}_{irep}'):
dim = 2**n_qubits
qr = QuantumRegister(n_qubits)
initializer = QuantumCircuit(qr)
target = random_statevector(dim)
initializer.initialize(target, qr)
uninit = initializer.data[0][0].definition
self._remove_resets(uninit)
disentangler = Operator(uninit.data[0][0])
zero = Statevector.from_int(0, dim)
evolve = zero @ disentangler
ewinston marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(target, evolve)

def test_global_phase_1q(self):
"""Test global phase preservation with some simple 1q statevectors"""
target_list = [Statevector([1j, 0]), Statevector([0, 1j]),
Statevector([1j/np.sqrt(2), 1j/np.sqrt(2)])]
n_qubits = 1
dim = 2**n_qubits
qr = QuantumRegister(n_qubits)
for target in target_list:
with self.subTest(i=target):
initializer = QuantumCircuit(qr)
initializer.initialize(target, qr)
disentangler = Operator(initializer.data[0][0].definition.data[1][0])
Cryoris marked this conversation as resolved.
Show resolved Hide resolved
zero = Statevector.from_int(0, dim)
actual = zero @ disentangler
self.assertEqual(target, actual)


class TestInstructionParam(QiskitTestCase):
"""Test conversion of numpy type parameters."""
Expand Down