Skip to content

Commit

Permalink
Remove opflow from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Sep 15, 2023
1 parent 3cccebd commit 5f4ecee
Showing 1 changed file with 4 additions and 105 deletions.
109 changes: 4 additions & 105 deletions test/unit/test_data_serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -20,41 +20,11 @@
from datetime import datetime

import numpy as np
import scipy.sparse
from qiskit.algorithms.optimizers import (
ADAM,
GSLS,
SPSA,
QNSPSA,
L_BFGS_B,
NELDER_MEAD,
)

from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.test.reference_circuits import ReferenceCircuits
from qiskit.circuit.library import EfficientSU2, CXGate, PhaseGate, U2Gate
from qiskit.opflow import (
PauliSumOp,
MatrixOp,
PauliOp,
CircuitOp,
EvolvedOp,
TaperedPauliSumOp,
Z2Symmetries,
I,
X,
Y,
Z,
StateFn,
CircuitStateFn,
DictStateFn,
VectorStateFn,
OperatorStateFn,
SparseVectorStateFn,
CVaRMeasurement,
ComposedOp,
SummedOp,
TensoredOp,
)

from qiskit.providers.fake_provider import FakeNairobi
from qiskit.quantum_info import SparsePauliOp, Pauli, Statevector
from qiskit.result import Result
Expand Down Expand Up @@ -123,76 +93,6 @@ def test_coder_qc(self):
decoded = [decoded]
self.assertTrue(all(isinstance(item, QuantumCircuit) for item in decoded))

def test_coder_operators(self):
"""Test runtime encoder and decoder for operators."""
coeff_x = Parameter("x")
coeff_y = coeff_x + 1
quantum_circuit = QuantumCircuit(1)
quantum_circuit.h(0)
operator = 2.0 * I ^ I
z2_symmetries = Z2Symmetries(
[Pauli("IIZI"), Pauli("ZIII")],
[Pauli("IIXI"), Pauli("XIII")],
[1, 3],
[-1, 1],
)
isqrt2 = 1 / np.sqrt(2)
sparse = scipy.sparse.csr_matrix([[0, isqrt2, 0, isqrt2]])

subtests = (
PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]), coeff=3),
PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[1]), coeff=coeff_y),
PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[1 + 2j]), coeff=3 - 2j),
PauliSumOp.from_list([("II", -1.052373245772859), ("IZ", 0.39793742484318045)]),
MatrixOp(primitive=np.array([[0, -1j], [1j, 0]]), coeff=coeff_x),
PauliOp(primitive=Pauli("Y"), coeff=coeff_x),
CircuitOp(quantum_circuit, coeff=coeff_x),
EvolvedOp(operator, coeff=coeff_x),
TaperedPauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]), z2_symmetries),
StateFn(quantum_circuit, coeff=coeff_x),
CircuitStateFn(quantum_circuit, is_measurement=True),
DictStateFn("1" * 3, is_measurement=True),
VectorStateFn(np.ones(2**3, dtype=complex)),
OperatorStateFn(CircuitOp(QuantumCircuit(1))),
SparseVectorStateFn(sparse),
Statevector([1, 0]),
CVaRMeasurement(Z, 0.2),
ComposedOp([(X ^ Y ^ Z), (Z ^ X ^ Y ^ Z).to_matrix_op()]),
SummedOp([X ^ X * 2, Y ^ Y], 2),
TensoredOp([(X ^ Y), (Z ^ I)]),
(Z ^ Z) ^ (I ^ 2),
)
for operator in subtests:
with self.subTest(operator=operator):
encoded = json.dumps(operator, cls=RuntimeEncoder)
self.assertIsInstance(encoded, str)
decoded = json.loads(encoded, cls=RuntimeDecoder)
self.assertEqual(operator, decoded)

def test_coder_optimizers(self):
"""Test runtime encoder and decoder for optimizers."""
subtests = (
(ADAM, {"maxiter": 100, "amsgrad": True}),
(GSLS, {"maxiter": 50, "min_step_size": 0.01}),
(SPSA, {"maxiter": 10, "learning_rate": 0.01, "perturbation": 0.1}),
(QNSPSA, {"fidelity": 123, "maxiter": 25, "resamplings": {1: 100, 2: 50}}),
# some SciPy optimizers only work with default arguments due to Qiskit/qiskit-terra#6682
(L_BFGS_B, {}),
(NELDER_MEAD, {}),
# Enable when https://github.com/scikit-quant/scikit-quant/issues/24 is fixed
# (IMFIL, {"maxiter": 20}),
# (SNOBFIT, {"maxiter": 200, "maxfail": 20}),
)
for opt_cls, settings in subtests:
with self.subTest(opt_cls=opt_cls):
optimizer = opt_cls(**settings)
encoded = json.dumps(optimizer, cls=RuntimeEncoder)
self.assertIsInstance(encoded, str)
decoded = json.loads(encoded, cls=RuntimeDecoder)
self.assertTrue(isinstance(decoded, opt_cls))
for key, value in settings.items():
self.assertEqual(decoded.settings[key], value)

def test_coder_noise_model(self):
"""Test encoding and decoding a noise model."""
noise_model = NoiseModel.from_backend(FakeNairobi())
Expand Down Expand Up @@ -268,8 +168,7 @@ def test_decoder_import(self):
temp_fp.close()

subtests = (
PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]), coeff=3),
DictStateFn("1" * 3, is_measurement=True),
SparsePauliOp(Pauli("XYZX"), coeffs=[2]),
Statevector([1, 0]),
)
for operator in subtests:
Expand Down

0 comments on commit 5f4ecee

Please sign in to comment.