def iplot_state_qsphere(rho, figsize=None):
<EOL>html_template = Template("""<STR_LIT>""")<EOL>javascript_template = Template("""<STR_LIT>""")<EOL>rho = _validate_input_state(rho)<EOL>if figsize is None:<EOL><INDENT>options = {}<EOL><DEDENT>else:<EOL><INDENT>options = {'<STR_LIT:width>': figsize[<NUM_LIT:0>], '<STR_LIT>': figsize[<NUM_LIT:1>]}<EOL><DEDENT>qspheres_data = []<EOL>num = int(np.log2(len(rho)))<EOL>weig, stateall = linalg.eigh(rho)<EOL>for _ in range(<NUM_LIT:2>**num):<EOL><INDENT>probmix = weig.max()<EOL>prob_location = weig.argmax()<EOL>if probmix > <NUM_LIT>:<EOL><INDENT>state = stateall[:, prob_location]<EOL>loc = np.absolute(state).argmax()<EOL>for j in range(<NUM_LIT:2>**num):<EOL><INDENT>test = np.absolute(np.absolute(state[j]) -<EOL>np.absolute(state[loc]))<EOL>if test < <NUM_LIT>:<EOL><INDENT>loc = j<EOL>break<EOL><DEDENT><DEDENT>angles = (np.angle(state[loc]) + <NUM_LIT:2> * np.pi) % (<NUM_LIT:2> * np.pi)<EOL>angleset = np.exp(-<NUM_LIT>*angles)<EOL>state = angleset*state<EOL>state.flatten()<EOL>spherepoints = []<EOL>for i in range(<NUM_LIT:2>**num):<EOL><INDENT>element = bin(i)[<NUM_LIT:2>:].zfill(num)<EOL>weight = element.count("<STR_LIT:1>")<EOL>number_of_divisions = n_choose_k(num, weight)<EOL>weight_order = bit_string_index(element)<EOL>angle = weight_order * <NUM_LIT:2> * np.pi / number_of_divisions<EOL>zvalue = -<NUM_LIT:2> * weight / num + <NUM_LIT:1><EOL>xvalue = np.sqrt(<NUM_LIT:1> - zvalue**<NUM_LIT:2>) * np.cos(angle)<EOL>yvalue = np.sqrt(<NUM_LIT:1> - zvalue**<NUM_LIT:2>) * np.sin(angle)<EOL>prob = np.real(np.dot(state[i], state[i].conj()))<EOL>angles = (np.angle(state[i]) + <NUM_LIT:2> * np.pi) % (<NUM_LIT:2> * np.pi)<EOL>qpoint = {<EOL>'<STR_LIT:x>': xvalue,<EOL>'<STR_LIT:y>': yvalue,<EOL>'<STR_LIT:z>': zvalue,<EOL>'<STR_LIT>': prob,<EOL>'<STR_LIT>': angles<EOL>}<EOL>spherepoints.append(qpoint)<EOL><DEDENT>sphere = {<EOL>'<STR_LIT>': spherepoints,<EOL>'<STR_LIT>': probmix<EOL>}<EOL>qspheres_data.append(sphere)<EOL>weig[prob_location] = <NUM_LIT:0><EOL><DEDENT><DEDENT>div_number = str(time.time())<EOL>div_number = re.sub('<STR_LIT>', '<STR_LIT>', div_number)<EOL>html = html_template.substitute({<EOL>'<STR_LIT>': div_number<EOL>})<EOL>javascript = javascript_template.substitute({<EOL>'<STR_LIT:data>': qspheres_data,<EOL>'<STR_LIT>': div_number,<EOL>'<STR_LIT>': options<EOL>})<EOL>display(HTML(html + javascript))<EOL>
Create a Q sphere representation.

        Graphical representation of the input array, using a Q sphere for each
        eigenvalue.

        Args:
            rho (array): State vector or density matrix.
            figsize (tuple): Figure size in pixels.