Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Fix QFT deprecations in HHL test #934

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion qiskit/aqua/circuits/phase_estimation_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def __init__(

# cannot check for IQFT type due to circular import
if not isinstance(iqft, QuantumCircuit):
warnings.warn('The qiskit.aqua.components.iqfts.IQFT module is deprecated as of 0.7.0 '
warnings.warn('Providing a qiskit.aqua.components.iqfts.IQFT module to the '
'PhaseEstimationCircuit is deprecated as of 0.7.0 '
'and will be removed no earlier than 3 months after the release. '
'You should pass a QuantumCircuit instead, see '
'qiskit.circuit.library.QFT and the .inverse() method.',
Expand Down
12 changes: 7 additions & 5 deletions qiskit/aqua/components/eigs/eigs_qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def __init__(self,
self._operator = op_converter.to_weighted_pauli_operator(operator)

if isinstance(iqft, IQFT):
warnings.warn('The qiskit.aqua.components.iqfts.IQFT module is deprecated as of 0.7.0 '
'and will be removed no earlier than 3 months after the release. '
warnings.warn('Providing a qiskit.aqua.components.iqfts.IQFT module as `iqft` argument '
'to HHL is deprecated as of 0.7.0 and will be removed no earlier than '
'3 months after the release. '
'You should pass a QuantumCircuit instead, see '
'qiskit.circuit.library.QFT and the .inverse() method.',
DeprecationWarning, stacklevel=2)
Expand All @@ -87,8 +88,9 @@ def __init__(self,
self._negative_evals = negative_evals

if ne_qfts and any(isinstance(ne_qft, IQFT) for ne_qft in ne_qfts):
warnings.warn('The qiskit.aqua.components.iqfts.IQFT module is deprecated as of 0.7.0 '
'and will be removed no earlier than 3 months after the release. '
warnings.warn('Providing a qiskit.aqua.components.iqfts.IQFT module in the `ne_qft` '
'argument to HHL is deprecated as of 0.7.0 and will be removed no '
'earlier than 3 months after the release. '
'You should pass a QuantumCircuit instead, see '
'qiskit.circuit.library.QFT and the .inverse() method.',
DeprecationWarning, stacklevel=2)
Expand Down Expand Up @@ -125,7 +127,7 @@ def get_scaling(self):
return self._evo_time

def construct_circuit(self, mode, register=None):
""" Construct the eigenvalues estimation using the PhaseEstimationCircuit
"""Construct the eigenvalues estimation using the PhaseEstimationCircuit

Args:
mode (str): construction mode, 'matrix' not supported
Expand Down
51 changes: 36 additions & 15 deletions test/aqua/test_hhl.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def tearDown(self):
def _create_eigs(matrix, num_ancillae, negative_evals, use_circuit_library=True):
# Adding an additional flag qubit for negative eigenvalues
ne_qfts = [None, None]
if not use_circuit_library:
warnings.filterwarnings('ignore', category=DeprecationWarning)

if negative_evals:
num_ancillae += 1
if use_circuit_library:
Expand All @@ -65,25 +68,27 @@ def _create_eigs(matrix, num_ancillae, negative_evals, use_circuit_library=True)
else:
iqft = StandardIQFTS(num_ancillae)

return EigsQPE(MatrixOperator(matrix=matrix),
iqft,
num_time_slices=1,
num_ancillae=num_ancillae,
expansion_mode='suzuki',
expansion_order=2,
evo_time=None,
negative_evals=negative_evals,
ne_qfts=ne_qfts)
eigs_qpe = EigsQPE(MatrixOperator(matrix=matrix),
iqft,
num_time_slices=1,
num_ancillae=num_ancillae,
expansion_mode='suzuki',
expansion_order=2,
evo_time=None,
negative_evals=negative_evals,
ne_qfts=ne_qfts)

if not use_circuit_library:
warnings.filterwarnings('always', category=DeprecationWarning)

return eigs_qpe

@data([[0, 1], False], [[1, 0], False], [[1, 0.1], False], [[1, 1], False], [[1, 10], False],
[[0, 1], True], [[1, 0], True], [[1, 0.1], True], [[1, 1], True], [[1, 10], True])
@unpack
def test_hhl_diagonal(self, vector, use_circuit_library):
""" hhl diagonal test """
self.log.debug('Testing HHL simple test in mode Lookup with statevector simulator')
if not use_circuit_library:
# ignore deprecation warnings from QFTs
warnings.filterwarnings(action="ignore", category=DeprecationWarning)

matrix = [[1, 0], [0, 1]]

Expand All @@ -108,9 +113,13 @@ def test_hhl_diagonal(self, vector, use_circuit_library):

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)
if not use_circuit_library:
warnings.filterwarnings('ignore', category=DeprecationWarning)
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))
if not use_circuit_library:
warnings.filterwarnings('always', category=DeprecationWarning)

hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)
Expand All @@ -124,9 +133,6 @@ def test_hhl_diagonal(self, vector, use_circuit_library):
self.log.debug('fidelity HHL to algebraic: %s', fidelity)
self.log.debug('probability of result: %s', hhl_result["probability_result"])

if not use_circuit_library:
warnings.filterwarnings(action="always", category=DeprecationWarning)

@data([[-1, 0], False], [[0, -1], False], [[-1, -1], False],
[[-1, 0], True], [[0, -1], True], [[-1, -1], True])
@unpack
Expand Down Expand Up @@ -157,9 +163,14 @@ def test_hhl_diagonal_negative(self, vector, use_circuit_library):

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)
if not use_circuit_library:
warnings.filterwarnings('ignore', category=DeprecationWarning)
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))
if not use_circuit_library:
warnings.filterwarnings('always', category=DeprecationWarning)

hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)

Expand Down Expand Up @@ -201,9 +212,12 @@ def test_hhl_diagonal_longdivison(self, vector):

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)
warnings.filterwarnings('ignore', category=DeprecationWarning)
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))
warnings.filterwarnings('always', category=DeprecationWarning)

hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)

Expand Down Expand Up @@ -246,9 +260,11 @@ def test_hhl_diagonal_qasm(self, vector):

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)
warnings.filterwarnings('ignore', category=DeprecationWarning)
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('qasm_simulator'), shots=1000,
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))
warnings.filterwarnings('always', category=DeprecationWarning)
hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)

Expand Down Expand Up @@ -339,6 +355,7 @@ def test_hhl_negative_eigs(self):
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))

hhl_solution = hhl_result["solution"]
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)

Expand Down Expand Up @@ -380,9 +397,12 @@ def test_hhl_random_hermitian(self):

algo = HHL(matrix, vector, truncate_powerdim, truncate_hermitian, eigs,
init_state, reci, num_q, num_a, orig_size)
warnings.filterwarnings('ignore', category=DeprecationWarning)
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))
warnings.filterwarnings('always', category=DeprecationWarning)

hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)

Expand Down Expand Up @@ -426,6 +446,7 @@ def test_hhl_non_hermitian(self):
hhl_result = algo.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=aqua_globals.random_seed,
seed_transpiler=aqua_globals.random_seed))

hhl_solution = hhl_result['solution']
hhl_normed = hhl_solution / np.linalg.norm(hhl_solution)
# compare result
Expand Down