Ghostly Images and Qubits: A New Way to Visualize Quantum Superposition | by Kory Becker | Mar, 2023


TUTORIAL

Explore the mysteries of quantum computing!

Source: Stable Diffusion.

Whenever I learn a new programming language or framework, I often find myself trying to program a game as a first try at using the various features of the platform.

In the case of quantum computing, creating the beginnings of a game in order to learn the concepts is absolutely perfect.

Now, quantum computing has a range of topics and features to learn. These may include how qubits are made, how qubits are manipulated, the various gates, but the focus of this tutorial will be the all-powerful concept of superposition!

Superposition is one of the core features in programming quantum computers, since it allows a qubit to hold a value of both 0 and 1 simultaneously.

When a qubit is in superposition it functions in a quantum state. This makes it powerful!

A classical computer with one bit can process one bit of information (0 or 1). By contrast, a quantum computer with one qubit can process two bits of information (0 and 1).

Similarly, two classical bits can process two bits of information, while two qubits extend this to processing four bits of information (00, 01, 10, 11).

Ultimately, this allows a quantum computer with n-qubits to process 2^n bits of information — all in a single CPU cycle.

A quantum computer with n-qubits in superposition can process 2^n states per cycle. Source: Author.

It can be quite amazing to consider that a quantum computer with just 55 qubits can process nearly 5 petabytes (2^55 bits) of information in a single cycle.

This could be the entire database of your favorite social media web site, processed in an instant!

Indeed, this is the amazing difference between classical and quantum computers, and is a result of the simultaneous nature of a qubit in superposition, also called quantum parallelism.

In fact, superposition allows for quadratic and exponential performance boosts from algorithms designed to leverage this unique quantum ability.

Of course, for our project, we will just need a single qubit.

In order to interpret and use the value from a qubit, we have to measure it.

This simple act of measuring a qubit results in a collapse of the quantum state, which changes the qubit from holding a value of both 0 and 1 simultaneously, to instead holding an exclusive value of 0 or 1 — just like a classical bit.

Since a measured qubit can now be interpreted as a value of 0 or 1, we can use the qubit just like you would with a classical computer.

Keep this in mind, as this is where the fun begins!

Visualizing a qubit in superposition is often performed using various diagrams including: q-spheres, phase disks, and state-vectors.

However, I wanted to try something more fun!

Since a qubit in superposition will hold a value of 0 and 1 simultaneously, I thought it would be fun to draw two images superimposed on top of one another.

The two images to be superimposed according to a qubit’s measurements. Source: Stable Diffusion.

Each of the two images will represent a state of either 0 or 1 that the qubit may be in. Depending upon the measurement of the qubit in the computational basis state and the percentage chance that the qubit will measure as a value of 0 or 1, we can adjust the transparency (or alpha blend) of the two images in order to show one more prominently than the other.

The act of blending the images together on top of one other helps create a beautiful effect that shows how a qubit behaves.

A qubit placed into superposition will have a default measurement chance of 50% to measure as a value of 0 and 50% to measure as a value of 1.

A qubit in superposition is essentially random.

However, we can partially invert a qubit towards either of the values. This results in the qubit measuring more frequently as a 0 or 1, depending upon the amount of inversion applied.

We’ll use this feature of quantum computing to control the image transparency. To do this, we need a to use a special kind of quantum gate.

Qubits can be inverted from a value of 0 to 1 or vice-versa by using the X-gate.

The X-gate is just like a classical computer’s NOT operator. It simply flips the value of a qubit.

However, we don’t want to just flip a qubit from 0 to 1, as that would simply flip our image immediately from one to the other, with no ghostly in between state. Instead, we want to show a gradual change from one image to the next, and all states in between.

Luckily, there are more than just a few gates in the quantum toolkit.

A qubit contains three axes that affect its measurement behavior. These include the X, Y, and Z-axis.

Smite-Meister, CC BY-SA 3.0, via Wikimedia Commons.

Certain quantum gates rotate about the various axes in differing ways, which end up affecting the resulting measurement of the qubit.

For example, the X-gate performs a 180-degree rotation around the X-axis, flipping the qubit value from 0 to 1 (or 1 to 0).

There are also the Y and Z-gates, which rotate about their respective axes in a similar fashion.

These gates all have a distinct impact on the resulting measurement of the qubit. However, there is one specific gate that provides us with the partial inversion effect that we’re looking for.

The U-gate is a special gate that takes three arguments. These include theta, phi, and lam. The arguments allow modifying the Euler angles of the qubit in order to rotate around the Bloch sphere.

The U-gate is actually a generalized version for single-qubit quantum gates and can even be used as a replacement, depending on the values for the arguments.

You can try adjusting the various arguments of the U-gate to see the resulting effect on the qubit measurements.

As it turns out, changing the theta argument allows us to skew the chance of a qubit measuring a 0 or 1 in exactly the fashion that we’re looking for!

The U-gate’s value for theta generally ranges from -1.5 to 1.5.

At the lower end of the range, the qubit measurements will result closer towards a measurement of zero. At the higher end of the range, measurements will be closer towards one. Any value between will result in a skew towards 0 or 1.

As an example, we could set the qubit to have a 20% chance of measuring as 0, and 80% chance as measuring 1, by setting the theta angle for the U-gate to 0.8.

Continuing to adjust the U-gate’s values, we can see how the qubit measurements result in various chances of holding a 0 or 1 according to the U-gate theta value.

Since the range for theta that we will be using is -1.5 to 1.5, if we set the rate for theta to 0.0, we will see a traditional 50/50 chance of measuring 0 or 1. However, if we set the value for theta to -0.6, we can see a skewed behavior, resulting in a much higher chance of measuring 0 than measuring 1.

The distribution of measurements from a qubit in superposition with partial inversion from a U-gate. The qubit has a much higher chance of measuring 0 than 1. Source: Author.

Similarly, if we set theta all the way to -1.5, we’ll have a near 100% chance of always measuring a 0. Likewise, setting the value to 1.5 will result in a measurement of 1.

With the mechanics out of the way, let’s put our program together!

We’ll start by creating a quantum program that places a single qubit into superposition.

Since the default nature of a qubit in superposition results in a 50/50 chance of measuring 0 or 1, we’ll use the U-gate to slightly nudge the qubit towards a specific direction.

We’ll adjust the transparency of our two images according to the measurements of the qubit, while sliding the theta argument for the U-gate up and down.

Our quantum circuit is quite simple. We’ll simply create a program that joins a Hadamard gate for placing the qubit into superposition, with a U-gate for applying the desired rotation.

# Create a quantum circuit with 1 qubit.
qc = QuantumCircuit(1)

# Initialize the simulator.
simulator = Aer.get_backend('aer_simulator')

# Place the qubit into superposition.
qc.h(0)

# Apply partial inversion to the qubit.
# If our range is 0.0 to 1.0, we can set: theta = rate * 3 - 1.5
qc.u(theta, 0, 0, 0)

# Measure the result.
qc.measure_all()

A qubit in superposition with partial inversion. The resulting measurement will skew towards 0 or 1 according to the U-gate theta value. Source: Author.

Since we want modify the rate of inversion over time as our images display, we can create a helper method for inverting the qubit.

Additionally, we’ll use a more natural rate parameter to control the inversion, which will range from 0.0 to 1.0, instead of the more awkward theta range of -1.5 to 1.5.

Finally, we’ll return the resulting qubit measurements.

def invert(rate):
# Convert the rate of inversion from [0.0 to 1.0]
# to a theta range of [-1.5 to 1.5] using the formula:
# ((old_value - old_min) / (old_max - old_min)) * (new_max - new_min) + new_min
theta = rate * 3 - 1.5

# Create a circuit.
qc = QuantumCircuit(1)

# Initialize the simulator.
simulator = Aer.get_backend('aer_simulator')

# Place the qubit into superposition.
qc.h(0)

# Apply partial inversion to the qubit.
qc.u(theta, 0, 0, 0)

# Measure the result.
qc.measure_all()

# Execute the circuit.
job = execute(qc, simulator)
result = job.result()

# Return the counts of 0 and 1.
return result.get_counts()

If we call the invert() method with a rate of 0.5, we’ll have the same old equal 50/50 chance of measuring a 0 or 1.

However, if we set the rate to 0.3, we’ll have a 70–80% chance of measuring a 0 compared to measuring a 1, as shown in the following result.

{‘0’: 792, ‘1’: 232}

We can slide the value for rate up or down to affect the measurement of the qubit accordingly.

Now that our quantum computing program is in place, we can use the qubit as our sole indicator to draw either the unicorn or the gremlin with greater opacity.

If there are more zero-measurements for the qubit, we’ll draw the unicorn in more clarity.

If there are more one-measurements for the qubit, we’ll draw the gremlin in more clarity.

If the qubit measures somewhere between the two, we’ll draw both images on top of each other with a blended transparency equal to the measurement from the qubit.

We’ll use the ipycanvas library for drawing and animating the images, in conjunction with our quantum computing program.

We begin by initializing a graphics canvas which will allow us to draw the images.

def init():
canvas = Canvas()
display(canvas)

canvas.fill_style = 'white'
canvas.fill_rect(0, 0, 300, 300)

canvas.fill_style = 'black'
canvas.fill_rect(5, 295, 290, 28)

return canvas

# Load sprites.
unicorn = Image.from_file("unicorn.jpg")
gremlin = Image.from_file("gremlin.jpg")

We’ll also need some helper methods for drawing the images and updating their transparency values.

We can do this by creating a draw() method which takes the two sprites as parameters (the unicorn and the gremlin), along with an alpha value for blending.

We’ll also create an update() method which calls our quantum computing program, applies partial inversion to the qubit, and uses the resulting measurement counts of 0 and 1 in order to set the blended image transparency.

def draw(sprite1, sprite2, alpha):
canvas.global_alpha = alpha
canvas.draw_image(sprite1, 5, 5, 290, 290)

canvas.global_alpha = 1 - alpha
canvas.draw_image(sprite2, 5, 5, 290, 290)

canvas.global_alpha = 1
canvas.font = "20px arial"
canvas.fill_style = '#00ff00'
canvas.fill_text('Qubit inverted ' + str(round(alpha, 1) * 100) + '%', 10, 316)

return canvas

def update(sprite1, sprite2, rate):
# Run the quantum program and obtain the qubit measurements.
counts = invert(rate)

# Get the counts for "unicorn" from the qubit measuring 0, and the gremlin for the qubit measuring 1.
alpha = counts['0'] / 1024 if '0' in counts else 0

draw(sprite1, sprite2, alpha)

Finally, let’s put it all together!

We’ll create an animate() method that simply loops multiple times, running the quantum program at each iteration, while gradually adjusting the inversion rate (theta) for the qubit up or down.

The result is a ghostly apparition effect, as the unicorn slowly changes into a gremlin, along with the resulting measurement of the qubits coinciding with 0 or 1.

def animate(rate = 0):
with hold_canvas():
for i in range(10):
show_unicorn = True

for j in range(20*2):
canvas.fill_style = 'black'
canvas.fill_rect(5, 295, 290, 28)

update(unicorn, gremlin, rate)
rate += 0.05 if show_unicorn else -0.05
if rate > 1:
show_unicorn = False
elif rate < 0:
show_unicorn = True

canvas.sleep(125)

canvas = init()
animate()

Behold — the results of our creation!

Superimposed images according to a qubit’s measurements. Source: Author, Stable Diffusion.

What a fascinating way to visualize the measurement of a qubit! Just imagine all of the possibilities that can be achieved through combining quantum computing with classical games. I hope this helps to stir your creativity in what can be done.

You can download the complete code example for the unicorn gremlin program here.

If you’ve enjoyed this article, please consider following me on Medium, Twitter, and my web site to be notified of my future posts and research work.




TUTORIAL

Explore the mysteries of quantum computing!

Source: Stable Diffusion.

Whenever I learn a new programming language or framework, I often find myself trying to program a game as a first try at using the various features of the platform.

In the case of quantum computing, creating the beginnings of a game in order to learn the concepts is absolutely perfect.

Now, quantum computing has a range of topics and features to learn. These may include how qubits are made, how qubits are manipulated, the various gates, but the focus of this tutorial will be the all-powerful concept of superposition!

Superposition is one of the core features in programming quantum computers, since it allows a qubit to hold a value of both 0 and 1 simultaneously.

When a qubit is in superposition it functions in a quantum state. This makes it powerful!

A classical computer with one bit can process one bit of information (0 or 1). By contrast, a quantum computer with one qubit can process two bits of information (0 and 1).

Similarly, two classical bits can process two bits of information, while two qubits extend this to processing four bits of information (00, 01, 10, 11).

Ultimately, this allows a quantum computer with n-qubits to process 2^n bits of information — all in a single CPU cycle.

A quantum computer with n-qubits in superposition can process 2^n states per cycle. Source: Author.

It can be quite amazing to consider that a quantum computer with just 55 qubits can process nearly 5 petabytes (2^55 bits) of information in a single cycle.

This could be the entire database of your favorite social media web site, processed in an instant!

Indeed, this is the amazing difference between classical and quantum computers, and is a result of the simultaneous nature of a qubit in superposition, also called quantum parallelism.

In fact, superposition allows for quadratic and exponential performance boosts from algorithms designed to leverage this unique quantum ability.

Of course, for our project, we will just need a single qubit.

In order to interpret and use the value from a qubit, we have to measure it.

This simple act of measuring a qubit results in a collapse of the quantum state, which changes the qubit from holding a value of both 0 and 1 simultaneously, to instead holding an exclusive value of 0 or 1 — just like a classical bit.

Since a measured qubit can now be interpreted as a value of 0 or 1, we can use the qubit just like you would with a classical computer.

Keep this in mind, as this is where the fun begins!

Visualizing a qubit in superposition is often performed using various diagrams including: q-spheres, phase disks, and state-vectors.

However, I wanted to try something more fun!

Since a qubit in superposition will hold a value of 0 and 1 simultaneously, I thought it would be fun to draw two images superimposed on top of one another.

The two images to be superimposed according to a qubit’s measurements. Source: Stable Diffusion.

Each of the two images will represent a state of either 0 or 1 that the qubit may be in. Depending upon the measurement of the qubit in the computational basis state and the percentage chance that the qubit will measure as a value of 0 or 1, we can adjust the transparency (or alpha blend) of the two images in order to show one more prominently than the other.

The act of blending the images together on top of one other helps create a beautiful effect that shows how a qubit behaves.

A qubit placed into superposition will have a default measurement chance of 50% to measure as a value of 0 and 50% to measure as a value of 1.

A qubit in superposition is essentially random.

However, we can partially invert a qubit towards either of the values. This results in the qubit measuring more frequently as a 0 or 1, depending upon the amount of inversion applied.

We’ll use this feature of quantum computing to control the image transparency. To do this, we need a to use a special kind of quantum gate.

Qubits can be inverted from a value of 0 to 1 or vice-versa by using the X-gate.

The X-gate is just like a classical computer’s NOT operator. It simply flips the value of a qubit.

However, we don’t want to just flip a qubit from 0 to 1, as that would simply flip our image immediately from one to the other, with no ghostly in between state. Instead, we want to show a gradual change from one image to the next, and all states in between.

Luckily, there are more than just a few gates in the quantum toolkit.

A qubit contains three axes that affect its measurement behavior. These include the X, Y, and Z-axis.

Smite-Meister, CC BY-SA 3.0, via Wikimedia Commons.

Certain quantum gates rotate about the various axes in differing ways, which end up affecting the resulting measurement of the qubit.

For example, the X-gate performs a 180-degree rotation around the X-axis, flipping the qubit value from 0 to 1 (or 1 to 0).

There are also the Y and Z-gates, which rotate about their respective axes in a similar fashion.

These gates all have a distinct impact on the resulting measurement of the qubit. However, there is one specific gate that provides us with the partial inversion effect that we’re looking for.

The U-gate is a special gate that takes three arguments. These include theta, phi, and lam. The arguments allow modifying the Euler angles of the qubit in order to rotate around the Bloch sphere.

The U-gate is actually a generalized version for single-qubit quantum gates and can even be used as a replacement, depending on the values for the arguments.

You can try adjusting the various arguments of the U-gate to see the resulting effect on the qubit measurements.

As it turns out, changing the theta argument allows us to skew the chance of a qubit measuring a 0 or 1 in exactly the fashion that we’re looking for!

The U-gate’s value for theta generally ranges from -1.5 to 1.5.

At the lower end of the range, the qubit measurements will result closer towards a measurement of zero. At the higher end of the range, measurements will be closer towards one. Any value between will result in a skew towards 0 or 1.

As an example, we could set the qubit to have a 20% chance of measuring as 0, and 80% chance as measuring 1, by setting the theta angle for the U-gate to 0.8.

Continuing to adjust the U-gate’s values, we can see how the qubit measurements result in various chances of holding a 0 or 1 according to the U-gate theta value.

Since the range for theta that we will be using is -1.5 to 1.5, if we set the rate for theta to 0.0, we will see a traditional 50/50 chance of measuring 0 or 1. However, if we set the value for theta to -0.6, we can see a skewed behavior, resulting in a much higher chance of measuring 0 than measuring 1.

The distribution of measurements from a qubit in superposition with partial inversion from a U-gate. The qubit has a much higher chance of measuring 0 than 1. Source: Author.

Similarly, if we set theta all the way to -1.5, we’ll have a near 100% chance of always measuring a 0. Likewise, setting the value to 1.5 will result in a measurement of 1.

With the mechanics out of the way, let’s put our program together!

We’ll start by creating a quantum program that places a single qubit into superposition.

Since the default nature of a qubit in superposition results in a 50/50 chance of measuring 0 or 1, we’ll use the U-gate to slightly nudge the qubit towards a specific direction.

We’ll adjust the transparency of our two images according to the measurements of the qubit, while sliding the theta argument for the U-gate up and down.

Our quantum circuit is quite simple. We’ll simply create a program that joins a Hadamard gate for placing the qubit into superposition, with a U-gate for applying the desired rotation.

# Create a quantum circuit with 1 qubit.
qc = QuantumCircuit(1)

# Initialize the simulator.
simulator = Aer.get_backend('aer_simulator')

# Place the qubit into superposition.
qc.h(0)

# Apply partial inversion to the qubit.
# If our range is 0.0 to 1.0, we can set: theta = rate * 3 - 1.5
qc.u(theta, 0, 0, 0)

# Measure the result.
qc.measure_all()

A qubit in superposition with partial inversion. The resulting measurement will skew towards 0 or 1 according to the U-gate theta value. Source: Author.

Since we want modify the rate of inversion over time as our images display, we can create a helper method for inverting the qubit.

Additionally, we’ll use a more natural rate parameter to control the inversion, which will range from 0.0 to 1.0, instead of the more awkward theta range of -1.5 to 1.5.

Finally, we’ll return the resulting qubit measurements.

def invert(rate):
# Convert the rate of inversion from [0.0 to 1.0]
# to a theta range of [-1.5 to 1.5] using the formula:
# ((old_value - old_min) / (old_max - old_min)) * (new_max - new_min) + new_min
theta = rate * 3 - 1.5

# Create a circuit.
qc = QuantumCircuit(1)

# Initialize the simulator.
simulator = Aer.get_backend('aer_simulator')

# Place the qubit into superposition.
qc.h(0)

# Apply partial inversion to the qubit.
qc.u(theta, 0, 0, 0)

# Measure the result.
qc.measure_all()

# Execute the circuit.
job = execute(qc, simulator)
result = job.result()

# Return the counts of 0 and 1.
return result.get_counts()

If we call the invert() method with a rate of 0.5, we’ll have the same old equal 50/50 chance of measuring a 0 or 1.

However, if we set the rate to 0.3, we’ll have a 70–80% chance of measuring a 0 compared to measuring a 1, as shown in the following result.

{‘0’: 792, ‘1’: 232}

We can slide the value for rate up or down to affect the measurement of the qubit accordingly.

Now that our quantum computing program is in place, we can use the qubit as our sole indicator to draw either the unicorn or the gremlin with greater opacity.

If there are more zero-measurements for the qubit, we’ll draw the unicorn in more clarity.

If there are more one-measurements for the qubit, we’ll draw the gremlin in more clarity.

If the qubit measures somewhere between the two, we’ll draw both images on top of each other with a blended transparency equal to the measurement from the qubit.

We’ll use the ipycanvas library for drawing and animating the images, in conjunction with our quantum computing program.

We begin by initializing a graphics canvas which will allow us to draw the images.

def init():
canvas = Canvas()
display(canvas)

canvas.fill_style = 'white'
canvas.fill_rect(0, 0, 300, 300)

canvas.fill_style = 'black'
canvas.fill_rect(5, 295, 290, 28)

return canvas

# Load sprites.
unicorn = Image.from_file("unicorn.jpg")
gremlin = Image.from_file("gremlin.jpg")

We’ll also need some helper methods for drawing the images and updating their transparency values.

We can do this by creating a draw() method which takes the two sprites as parameters (the unicorn and the gremlin), along with an alpha value for blending.

We’ll also create an update() method which calls our quantum computing program, applies partial inversion to the qubit, and uses the resulting measurement counts of 0 and 1 in order to set the blended image transparency.

def draw(sprite1, sprite2, alpha):
canvas.global_alpha = alpha
canvas.draw_image(sprite1, 5, 5, 290, 290)

canvas.global_alpha = 1 - alpha
canvas.draw_image(sprite2, 5, 5, 290, 290)

canvas.global_alpha = 1
canvas.font = "20px arial"
canvas.fill_style = '#00ff00'
canvas.fill_text('Qubit inverted ' + str(round(alpha, 1) * 100) + '%', 10, 316)

return canvas

def update(sprite1, sprite2, rate):
# Run the quantum program and obtain the qubit measurements.
counts = invert(rate)

# Get the counts for "unicorn" from the qubit measuring 0, and the gremlin for the qubit measuring 1.
alpha = counts['0'] / 1024 if '0' in counts else 0

draw(sprite1, sprite2, alpha)

Finally, let’s put it all together!

We’ll create an animate() method that simply loops multiple times, running the quantum program at each iteration, while gradually adjusting the inversion rate (theta) for the qubit up or down.

The result is a ghostly apparition effect, as the unicorn slowly changes into a gremlin, along with the resulting measurement of the qubits coinciding with 0 or 1.

def animate(rate = 0):
with hold_canvas():
for i in range(10):
show_unicorn = True

for j in range(20*2):
canvas.fill_style = 'black'
canvas.fill_rect(5, 295, 290, 28)

update(unicorn, gremlin, rate)
rate += 0.05 if show_unicorn else -0.05
if rate > 1:
show_unicorn = False
elif rate < 0:
show_unicorn = True

canvas.sleep(125)

canvas = init()
animate()

Behold — the results of our creation!

Superimposed images according to a qubit’s measurements. Source: Author, Stable Diffusion.

What a fascinating way to visualize the measurement of a qubit! Just imagine all of the possibilities that can be achieved through combining quantum computing with classical games. I hope this helps to stir your creativity in what can be done.

You can download the complete code example for the unicorn gremlin program here.

If you’ve enjoyed this article, please consider following me on Medium, Twitter, and my web site to be notified of my future posts and research work.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
artificial intelligenceBeckerGhostlyImagesKoryMARQuantumqubitsSuperpositionTechnoblenderTechnologyvisualize
Comments (0)
Add Comment