<jupyter_start><jupyter_text>Quantum Dynamic Pricing Engine**Backend/Quantum Annealing Optimization** Required imports:<jupyter_code>from dwave.system import EmbeddingComposite, DWaveSampler
import neal
import numpy as np
import pandas as pd

from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>**Placeholder:** Randomly define bitstring of each cluster (bitstring length =  of products,  of bitstrings =  of clusters)<jupyter_code>num_clusters = 1
num_columns = 6

bitstrings = set()
while len(bitstrings) < num_clusters:
    a = tuple(np.random.choice([0, 1], size=(num_columns)))
    if a not in bitstrings: bitstrings.add(a)
bitstrings = np.array([list(x) for x in bitstrings])
bitstrings = np.reshape(bitstrings, (bitstrings.shape[0], 1, bitstrings.shape[1]))
bitstrings<jupyter_output><empty_output><jupyter_text>**For this case study, simply set bitstrings to string below**<jupyter_code>bitstrings = np.array([[[0, 1, 1, 1, 0, 1]]])<jupyter_output><empty_output><jupyter_text>**Construct:** Create interaction dictionary per cluster bitstring<jupyter_code>interaction_dicts = []

for bs in bitstrings:
    interaction = bs.T @ bs
    interaction = interaction - np.diagflat(np.diag(interaction))
#     interaction = np.where(interaction == 0, -1, interaction)
    interaction = interaction / bitstrings.shape[2]
    J = {(i, j) : col for i, row in enumerate(interaction) for j, col in enumerate(row)}
    interaction_dicts.append(J)
interaction<jupyter_output><empty_output><jupyter_text>**Execute:** Execute annealing per interaction dictionary<jupyter_code>solver = neal.SimulatedAnnealingSampler()
samples_across_Js = []
a = -0.01 # [-0.1, -0.4, -1]
base = -0.01
h0 = {0:a, 1:a, 2:a, 3:a, 4:a, 5:a}
h1 = {0:base, 1:a, 2:base, 3:base, 4:base, 5:base}

# a = -0.02 # [-0.1, -0.3, -1]
# index = 12
# # h0 = {k: a for k in range(groccenters_inv.shape[2])}
# h1 = {k: -0.01 for k in range(groccenters_inv.shape[2])}
# h1[index] = a

j = 0
for J in [interaction_dicts[0]]:
    sampleset = solver.sample_ising(h1, J,
                                 num_reads = 1000,
                                 label='Dynamic Quantum Pricing')
    samples_across_Js.append(sampleset)
    print(j)
    j = j + 1<jupyter_output>0<jupyter_text>**Results**<jupyter_code>pd_sampleset_small_uniform_0 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_small_uniform_0
pd_sampleset_small_uniform_0.sum(axis=0)
pd_sampleset_small_uniform_1 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_small_uniform_1
pd_sampleset_small_uniform_1.sum(axis=0)
pd_sampleset_small_uniform_2 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_small_uniform_2
pd_sampleset_small_uniform_2.sum(axis=0)
plt.figure(figsize=(20,6))
plt.subplot(1, 3, 1)
pd_sampleset_small_uniform_0.sum(axis=0)[:6].plot.bar()
plt.title('No Discount')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')
plt.subplot(1, 3, 2)
pd_sampleset_small_uniform_1.sum(axis=0)[:6].plot.bar()
plt.title('10% Discount Sale')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')
plt.subplot(1, 3, 3)
pd_sampleset_small_uniform_2.sum(axis=0)[:6].plot.bar()
plt.title('20% Discount Sale')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')
pd_sampleset_index1_0 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_index1_0
pd_sampleset_index1_0.sum(axis=0)
pd_sampleset_index1_1 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_index1_1
pd_sampleset_index1_1.sum(axis=0)
pd_sampleset_index1_2 = samples_across_Js[0].to_pandas_dataframe()
pd_sampleset_index1_2
pd_sampleset_index1_2.sum(axis=0)
plt.figure(figsize=(20,6))
plt.subplot(1, 3, 1)
pd_sampleset_index1_0.sum(axis=0)[0:6].plot.bar()
plt.title('No Discount')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')
plt.subplot(1, 3, 2)
pd_sampleset_index1_1.sum(axis=0)[0:6].plot.bar()
plt.title('10% Discount on Product')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')
plt.subplot(1, 3, 3)
pd_sampleset_index1_2.sum(axis=0)[0:6].plot.bar()
plt.title('20% Discount on Product')
plt.xlabel('Product No.')
plt.ylabel('Buying Incentive')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14092664092664092, 'fraction_numerical': 0.03933397683397683, 'mean_word_length': 6.297535211267606, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '338704', 'license': 'MIT', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1627, 'n_words': 288, 'path': 'Quantum Finance/Quantum Dynamic Pricing Engine.ipynb', 'repo_name': 'ushnishray/Hackathon2021', 'size': '4144'}