# Plotting the GHZ state vectors

**URL:** <https://discuss.pennylane.ai/t/plotting-the-ghz-state-vectors/3034>\
**Category:** PennyLane Help\
**Created:** [June 10, 2023, 7:58pm UTC](https://discuss.pennylane.ai/t/plotting-the-ghz-state-vectors/3034 "2023-06-10T19:58:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Solomon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/solomon/32/1706_2.png) [@Solomon](https://discuss.pennylane.ai/u/Solomon)\
**Post date:** [June 10, 2023, 7:58pm UTC](https://discuss.pennylane.ai/t/plotting-the-ghz-state-vectors/3034/1 "2023-06-10T19:58:27Z")

</div>

Hello,  
I am trying to reproduce the results of my own paper ([https://arxiv.org/pdf/2302.12889.pdf](https://arxiv.org/pdf/2302.12889.pdf)) in which I used Yao.jl, in PennyLane. Here is the code snippet:

```auto
import pennylane as qml
import plotly.graph_objects as go
from IPython.display import display
from tqdm import tqdm
import time

# Quantum circuit and device setup
wires = 2
dev4 = qml.device("default.qubit", wires=wires, shots=1000)

def Q_Plot(cirq_0):
    fig, ax = qml.draw_mpl(cirq_0, expansion_strategy='device')()
    print(qml.draw(cirq_0, expansion_strategy='device')())
    # plt.show()
    fig.show()

@qml.qnode(dev4)
def ghz_circuit():
    qml.Hadamard(wires=0)
    qml.CNOT(wires=[0, 1])
    # qml.CNOT(wires=[0, 2])

    return qml.state()

# Initialize list to store results
all_results = []

# Create figure and histogram plots for the real and imaginary parts
fig_real = go.FigureWidget()
fig_real.add_bar(x=[f'Basis {i}' for i in range(2 **wires)], y=[0] * (2** wires))
fig_real.update_layout(xaxis_title='Basis State', yaxis_title='Probability', title='Distribution of Real Part')

fig_imag = go.FigureWidget()

# Display the figures
display(fig_real)

# Generate N=1000 calls to the quantum circuit and plot their distribution in real time
N = 100
for _ in tqdm(range(N)):
    result = ghz_circuit()
    all_results.append(result)

    # Separate the real and imaginary parts of the state vector
    probs_real = abs(result.real) ** 2
    # probs_imag = abs(result.imag) ** 2

    # Update the histogram plots with new data
    fig_real.data[0].y = probs_real
    # fig_imag.data[0].y = probs_imag

    # Update the plot layout
    fig_real.update_layout(title=f'Distribution of Real Part ({len(all_results)}/{N})')
    # fig_imag.update_layout(title=f'Distribution of Imaginary Part ({len(all_results)}/{N})')

    # Pause to allow for interactive plotting
    time.sleep(0.01)

    
Q_Plot(ghz_circuit)

```

This is the resulting plot:

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/4/46042ec7658d182ae0179d32506624d5be64d2cc.png)

And this is what I am trying to plot (from my paper):

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/e/e878b6a52aa068489193048809fca63c50955901.jpeg)

Is there a better / built-in method to do this?  
Thanks.

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [June 12, 2023, 6:42pm UTC](https://discuss.pennylane.ai/t/plotting-the-ghz-state-vectors/3034/2 "2023-06-12T18:42:06Z")

</div>

Hey @Solomon!

You can use [`qml.counts`](https://docs.pennylane.ai/en/stable/code/api/pennylane.counts.html?highlight=qml.counts)! There are good examples in the link I attached 🙂. Let me know if this helps!
