Comments (6)
I created a simpler sample to recreate the error. It seems to only happen for python 3.7 and it seems to be caused by Aer.
I couldn't recreate the error with BasicAer.
I could't recreate the error with python 3.8 either.
@woodsp-ibm couldn't recreate the error on Windows with python 3.7.
For python 3.7 It failed on Ubuntu after 14 and 116 iterations and on MacOS after 161 iterations.
import numpy as np
from qiskit import Aer, QuantumCircuit, BasicAer
from qiskit.algorithms.optimizers import COBYLA
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier
from qiskit_machine_learning.neural_networks import CircuitQNN
from qiskit_machine_learning.utils.loss_functions import CrossEntropyLoss
def _classifier_with_circuit_qnn_and_cross_entropy():
count = 0
while True:
algorithm_globals.random_seed = 12345
quantum_instance = QuantumInstance(
# BasicAer.get_backend("qasm_simulator"),
Aer.get_backend("aer_simulator"),
shots=100,
seed_simulator=algorithm_globals.random_seed,
seed_transpiler=algorithm_globals.random_seed,
)
optimizer = COBYLA(maxiter=25)
loss = CrossEntropyLoss()
num_inputs = 2
feature_map = ZZFeatureMap(num_inputs)
ansatz = RealAmplitudes(num_inputs, reps=1)
# construct circuit
qc = QuantumCircuit(num_inputs)
qc.append(feature_map, range(2))
qc.append(ansatz, range(2))
# construct qnn
def parity(x):
return "{:b}".format(x).count("1") % 2
output_shape = 2
qnn = CircuitQNN(
qc,
input_params=feature_map.parameters,
weight_params=ansatz.parameters,
sparse=False,
interpret=parity,
output_shape=output_shape,
quantum_instance=quantum_instance,
)
# classification may fail sometimes, so let's fix initial point
initial_point = np.array([0.5] * ansatz.num_parameters)
# construct classifier - note: CrossEntropy requires eval_probabilities=True!
classifier = NeuralNetworkClassifier(
qnn,
optimizer=optimizer,
loss=loss,
one_hot=True,
initial_point=initial_point,
# callback=None,
)
# construct data
num_samples = 5
x = algorithm_globals.random.random(
(num_samples, num_inputs)
)
y = 1.0 * (np.sum(x, axis=1) <= 1)
y = np.array([y, 1 - y]).transpose()
# fit to data
classifier.fit(x, y)
# score
score = classifier.score(x, y)
count += 1
print(f"\n{score} {initial_point} {x} {count}")
if score > 0.5:
print("Success")
else:
print("Failed")
return
if __name__ == "__main__":
_classifier_with_circuit_qnn_and_cross_entropy()
from qiskit-machine-learning.
Also, this unit test method in the same file failed too under python 3.7 or python 3.8:
test.algorithms.classifiers.test_neural_network_classifier.TestNeuralNetworkClassifier.test_classifier_with_opflow_qnn_19___bfgs____squared_error____statevector___True_ [5.494827s] ... FAILED
Captured traceback:
~~~~~~~~~~~~~~~~~~~
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.7.11/x64/lib/python3.7/site-packages/ddt.py", line 182, in wrapper
return func(self, *args, **kwargs)
File "/home/runner/work/qiskit-machine-learning/qiskit-machine-learning/test/algorithms/classifiers/test_neural_network_classifier.py", line 127, in test_classifier_with_opflow_qnn
self.assertGreater(score, 0.5)
File "/opt/hostedtoolcache/Python/3.7.11/x64/lib/python3.7/unittest/case.py", line 1251, in assertGreater
self.fail(self._formatMessage(msg, standardMsg))
File "/opt/hostedtoolcache/Python/3.7.11/x64/lib/python3.7/unittest/case.py", line 693, in fail
raise self.failureException(msg)
AssertionError: 0.4 not greater than 0
I created a sample script for the error and was able to reproduce in Ubuntu python 3.7 and MacOS python 3.7/3.8. Unfortunately for this method, I was able to recreate the error with Aer and BasicAer.
import numpy as np
from qiskit import Aer, BasicAer
from qiskit.algorithms.optimizers import L_BFGS_B
from qiskit.circuit.library import RealAmplitudes
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier
from qiskit_machine_learning.neural_networks import TwoLayerQNN, CircuitQNN
from qiskit_machine_learning.utils.loss_functions import CrossEntropyLoss
def _classifier_with_opflow_qnn():
loss = "squared_error"
count = 0
while True:
algorithm_globals.random_seed = 12345
quantum_instance = QuantumInstance(
# BasicAer.get_backend("statevector_simulator"),
Aer.get_backend("aer_simulator_statevector"),
seed_simulator=algorithm_globals.random_seed,
seed_transpiler=algorithm_globals.random_seed,
)
optimizer = L_BFGS_B(maxiter=5)
history = {"weights": [], "values": []}
def callback(objective_weights, objective_value):
history["weights"].append(objective_weights)
history["values"].append(objective_value)
num_inputs = 2
ansatz = RealAmplitudes(num_inputs, reps=1)
qnn = TwoLayerQNN(num_inputs, ansatz=ansatz, quantum_instance=quantum_instance)
initial_point = np.array([0.5] * ansatz.num_parameters)
classifier = NeuralNetworkClassifier(
qnn, optimizer=optimizer, loss=loss, initial_point=initial_point, callback=callback
)
# construct data
num_samples = 5
x = algorithm_globals.random.random(
(num_samples, num_inputs)
)
y = 2.0 * (np.sum(x, axis=1) <= 1) - 1.0
# fit to data
classifier.fit(x, y)
# score
score = classifier.score(x, y)
count += 1
print(f"\n{score} {initial_point} {x} {count}")
if score > 0.5:
print("Success")
else:
print("Failed")
return
if __name__ == "__main__":
_classifier_with_opflow_qnn()
from qiskit-machine-learning.
I have not seen this issue for a long time. If I recall correctly randomization was fixed in the unit tests and perhaps some randomization was also fixed in simulators.
@woodsp-ibm should we close it for now?
from qiskit-machine-learning.
As @manoelmarques sees info from nightlies etc maybe he has some thought. On my part if its not been seen in a while I would say close it it - if it starts again this or another new issue can always be (re-)opened etc.
from qiskit-machine-learning.
@adekusar-drl @woodsp-ibm I haven't seen any random error in the nightly builds in months. Also, I ran both samples above locally in my MacOS and they didn't fail even after more than 600 iterations while they would fail fast before.
We can close this issue. The problem seems to have been fixed.
from qiskit-machine-learning.
@manoelmarques Thanks for looking into this. Closing the issue.
from qiskit-machine-learning.
Related Issues (20)
- ISA circuit support for latest Runtime HOT 4
- Enhancement of PyTorch connector HOT 2
- Extend unit test coverage with `Hypothesis` in numerical tests
- Add `jit` compilation to the Torch connector with `thunder`
- Revamp `README.md` with structured information HOT 4
- Set up a security policy (@maintainers)
- Multi-class Classification Problem Using QSVC HOT 3
- Error when testing samples with labels other than {0, 1} in the MNIST dataset. HOT 6
- Revert CI environment to latest PyTorch once UTF bug is fixed
- Binary classification problem using NeuralNetworkClassifier and cross entropy loss HOT 1
- MacOS in CI - macos-latest is now ARM HOT 2
- Link Qiskit 1.0 migration instructions in Readme
- Add support for EstimatorV2 from ibm-qiskit-runtime to run circuits over hardware HOT 1
- Migrate `qiskit_algorithms` following end-of-support HOT 2
- Pinned `torch==2.2.2` breaks CI due to `numpy>=2.0`
- NeuralNetworkClassifier Accuracy Updates HOT 2
- Revert Numpy to the latest version in CI environment once UTF bug in PyTorch is fixed
- Restore mypy checks on Windows and lowest Numpy version
- The return values from SamplerQNN are in the wrong shape.
- Separate parameters for the trainable part and the encoding part in EstimatorQNN
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qiskit-machine-learning.