Trying to build something similar to demo MIMO decoding, getting error with my code bqm
Hi, I stumbled across an interesting application of DWave QPUs which is related to telecommunication, the demo provided is for coordinated multipoint MIMO decoder, I want to expand it using NOMA approach I have written code which should be correct based on qubo model found in research paper,
However I am facing an error.. below is the code and under it is the error.
Any help is appreciated!
from dwave.system import LeapHybridSampler
import dimod
import numpy as np
import matplotlib.pyplot as plt
# *** NOMA Parameters ***
num_users = 2 # Example with two NOMA users
modulation_order = 2 # BPSK modulation
channel_gains = np.array([0.8, 1.2])
# *** Plot BER vs. Noise Variance ***
noise_variances = np.linspace(0.05, 0.5, 10) # Range of noise variances to test
ber_results = []
for noise_var in noise_variances:
# *** Generate received signal (example) ***
transmitted_symbols = np.random.choice([-1, 1], size=num_users) # Generate random BPSK symbols
received_signal = np.dot(channel_gains, transmitted_symbols) + np.random.randn(num_users) * np.sqrt(noise_var)
# *** Construct the BQM ***
bqm = dimod.BinaryQuadraticModel(dimod.Vartype.BINARY)
# Linear terms
for i in range(num_users):
bqm.add_linear_from({i: -2 * received_signal[i] * channel_gains[i]})
# Quadratic terms
for i in range(num_users):
for j in range(i + 1, num_users):
bqm.add_quadratic_from({i: i, j: j, (i, j): 2 * channel_gains[i] * channel_gains[j]})
# *** Solve on the D-Wave QPU ***
sampler = LeapHybridSampler()
response = sampler.sample(bqm)
best_sample = response.first.sample
detected_symbols = list(best_sample.values())
# *** Calculate Bit Error Rate (BER) ***
num_errors = np.sum(np.abs(transmitted_symbols - detected_symbols))
ber = num_errors / len(transmitted_symbols)
ber_results.append(ber)
plt.plot(noise_variances, ber_results)
plt.xlabel("Noise Variance")
plt.ylabel("Bit Error Rate (BER)")
plt.title("NOMA Signal Detection (Quantum Annealing)")
plt.show()
ValueError Traceback (most recent call last)
Cell In[12], line 30
28 for i in range(num_users):
29 for j in range(i + 1, num_users):
---> 30 bqm.add_quadratic((i, j), np.dot(channel_gains[i], channel_gains[j]), 0.0) # Use np.dot
32 # *** Solve on the D-Wave QPU ***
33 sampler = LeapHybridSampler()
File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/dimod/decorators.py:404, in forwarding_method..wrapper(obj, *args, **kwargs)
399 except TypeError:
400 raise TypeError(
401 f"the '__dict__' attribute of {type(obj).__name__!r} "
402 "instance does not support item assignment.") from None
--> 404 return method(*args, **kwargs)
File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/dimod/binary/cybqm/cybqm_template.pyx.pxi:200, in dimod.binary.cybqm.cybqm_float64.cyBQM_template.add_quadratic()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" and now you should be able to solve it with all of our requirments
Comments
Hello,
Please format code and stack traces so they appear on separate lines as would be seen in a code editor for clearer comprehension and ease of assistance. This is also important for other users' legibility, and will avoid posts being removed for moderation.
Taking a look at the error, it looks like the np.dot function is returning an array. Looking at the add_quadratic function the quadratic bias term should be a float.
In cases like this it's often helpful to break apart more complex lines of code and make sure the expected values returned are the values being returned by the code.
Hopefully this helps get you unstuck. Please don't hesitate to ask more questions.
Please sign in to leave a comment.