<jupyter_start><jupyter_text>Learning Quantum Computing![../../../imgs/banner-1.jpg](../../../imgs/banner-1.jpg) IBM Q Experience/Qiskit Tutorials![../../imgs/qiskit-logo-banner.png](../../imgs/qiskit-logo-banner.png) Schrödinger's Cat Experiment (Simple and Random Experiment Tutorial)![imgs/ruben-cat-amazed-exclamation-points-1.png](imgs/ruben-cat-amazed-exclamation-points-1.png) Here, it will be shown a simple demonstration of the [**_Schröndiger's Cat Experiment_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat), using the [**_IBM Q Experience_**](https://www.research.ibm.com/ibm-q/technology/experience/) and the [**_Qiskit_**](https://qiskit.org/) _framework_ and its tools!!! ⚛️ 💥 ⚙️<jupyter_code># Learning Quantum Computing

# IBM Q Experience/Qiskit Tutorials

# Author:
# - <NAME>

# MSc. of Computer Science and Engineering

# Department of Informatics

# Faculty of Sciences and Technology of
# New University of Lisbon (FCT NOVA | FCT/UNL), Portugal
# Chapter 2 - Fundamentals
# Lesson b) Schrödinger's Cat Experience - Simple

# The basic elements needed for your first program are:
# - Quantum Circuit
# - Quantum Register<jupyter_output><empty_output><jupyter_text>Here, it will be imported all necessary _libraries_ and _tools_ from [**_Qiskit_**](https://qiskit.org/), as also, update the account of the current user in the [**_IBM Q Experience_**](https://www.research.ibm.com/ibm-q/technology/experience/):<jupyter_code># Import all libraries and tools from Qiskit
from qiskit import *
from qiskit.providers.ibmq import least_busy
from qiskit.tools.visualization import iplot_histogram
from qiskit.tools.visualization import plot_histogram

# Import the IBM Q Experience
from qiskit import IBMQ

# Update the account of the current user in the IBM Q Experience
IBMQ.update_account()<jupyter_output>Found 1 credentials.
No credentials available for updating could be found. No action will be performed.
Warnings:
The stored account with url "https://auth.quantum-computing.ibm.com/api" is already an IBM Q Experience v2 account.<jupyter_text>Then, it will be got and/or loaded a Provider of [**_Quantum Computers_**](https://en.wikipedia.org/wiki/Quantum_computing), to execute this program and simulation and list the currently available **_Backends_** (i.e., the currently available [**_Quantum Computers_**](https://en.wikipedia.org/wiki/Quantum_computing)), to execute this program and simulation:<jupyter_code># Get a provider of Quantum Computers,
# to execute this program and simulation (only used for the first time)
# - Note: Uncomment if you need
#my_provider = IBMQ.get_provider()

# Load the previously defined provider of Quantum Computers,
# to execute this program and simulation
my_provider = IBMQ.load_account()

# List the currently available Backends (i.e., the currently available Quantum Computers),
# to execute this program and simulation
my_provider.backends()
backend = my_provider.get_backend('ibmq_qasm_simulator')<jupyter_output><empty_output><jupyter_text>Now, it will be selected and used the least busy [**_Quantum Computer_**](https://en.wikipedia.org/wiki/Quantum_computing) from the [**_IBM Q Experience_**](https://www.research.ibm.com/ibm-q/technology/experience/)'s Provider as **_Backend_**...<jupyter_code># Use as backend the least busy Backend (Quantum Computer) from
# IBM Q' Experience's Provider
# - Note: Uncomment if you need
#backend = least_busy(IBMQ.backends(simulator = False))

# Print the name of the least busy Backend from
# the currently using IBM Q Experience's Provider
print("We'll use the least busy or default device: ", backend.name())<jupyter_output>We'll use the least busy or default device:  ibmq_qasm_simulator<jupyter_text>Starting the Experiment...At this point, we're already ready to start to build our [**_Quantum Circuit_**](https://en.wikipedia.org/wiki/Quantum_circuit) of the [**_Quantum System/State_**](https://en.wikipedia.org/wiki/Quantum_system) from the [**_Schröndiger's Cat Experiment_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat) to be possible start the _experiment_ and interpret the _results_ from it...  Creating the Quantum CircuitHere, it will be built our [**_Quantum Circuit_**](https://en.wikipedia.org/wiki/Quantum_circuit) of the [**_Quantum System/State_**](https://en.wikipedia.org/wiki/Quantum_system) from the [**_Schröndiger's Cat Experiment_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat). This [**_Quantum Circuit_**](https://en.wikipedia.org/wiki/Quantum_circuit) can be viewed as the **_Black/Mysterious Box_**, where the cat will be locked inside and also, where it will occur the most of the experiment... 📦 ❓<jupyter_code># Create a Quantum Register with 1 qubit,
# with the name of 'quantum_cat_state'
quantum_cat_state = QuantumRegister(1, 'cat_q')

# Create a Classical Register with 1 bit,
# with the name of 'classical_cat_state'
classical_cat_state = ClassicalRegister(1, 'cat_c')

# Create a program for a Quantum Circuit from
# a Quantum Register and a Classical Register
black_mysterious_box = QuantumCircuit(quantum_cat_state, classical_cat_state)<jupyter_output><empty_output><jupyter_text>Now, the [**_Quantum Circuit_**](https://en.wikipedia.org/wiki/Quantum_circuit) representing the **_Black/Mysterious Box_**, where the cat will be locked inside, will be in a [**_Quantum Superposition_**](https://en.wikipedia.org/wiki/Quantum_superposition)!!! 📦 ⚛️<jupyter_code># Quantum Circuit in Superposition, i.e., the Black/Mysterious Box 
black_mysterious_box = QuantumCircuit(quantum_cat_state, classical_cat_state)
black_mysterious_box.h(quantum_cat_state)<jupyter_output><empty_output><jupyter_text>And then, it will be made the Observer's Effect and the respectively [**_Measurement_**](https://en.wikipedia.org/wiki/Measurement_in_quantum_mechanics)... 🧐 🔎<jupyter_code>black_mysterious_box.measure(quantum_cat_state[0], classical_cat_state[0])

black_mysterious_box.draw()<jupyter_output><empty_output><jupyter_text>Caption/Notes:* **catq**:> The [**_Quantum State_**](https://en.wikipedia.org/wiki/Quantum_state) of the [**_Schröndiger's Cat Experiment_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat), in [**_Quantum Superposition_**](https://en.wikipedia.org/wiki/Quantum_superposition), inside the **_Black/Mysterious Box_**, while the two possibilities are occurring simultaneously (i.e., the [**_Schröndiger's Cat_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat) being **_alive_** and **_dead_** at the same time);* ***catc***:> The **_Classical State_** of the [**_Schröndiger's Cat Experiment_**](https://en.wikipedia.org/wiki/Schr%C3%B6dinger%27s_cat), after opening the **_Black/Mysterious Box_**, performing the [**_Observer's Effect_**](https://en.wikipedia.org/wiki/Observer_effect_(physics)) and the respectively [**_Measurement_**](https://en.wikipedia.org/wiki/Measurement_in_quantum_mechanics); At last, the job associated to this experiment will be executed in a [**_Quantum Computer_**](https://en.wikipedia.org/wiki/Quantum_computing) will be executed and its result be counted _1000_ times, in order to obtain a **_distribution of probabilities_** for the experiment...<jupyter_code>job = execute(black_mysterious_box, backend, shots = 1000)
result = job.result()<jupyter_output><empty_output><jupyter_text>Interpreting the results from the experimentNow, since the experiment it's finally over **_we can interpret its results_**, **_analysing the distribution of probabilites for the experiment and its respectively histogram's plot_**...<jupyter_code>schrodinger_cat_experiment_data = result.get_counts(black_mysterious_box)

result_counts = result.get_counts(black_mysterious_box)

print('Data Results from the Schrödinger\'s Cat Experiment:')
print('- Dead (State 0): ' + str(result_counts['0']) + ';')
print('- Alive (State 1): ' + str(result_counts['1']) + ';')

schrodinger_cat_experiment_legend = ['Possible States of \nthe Schrödinger\'s Cat,\nafter opening the\nBlack/Mysterious Box\n(Alive/State 1 and Dead/State 0)']
schrodinger_cat_experiment_title = 'The Schröndiger\'s Cat Experiment - Histogram'
schrodinger_cat_experiment = plot_histogram({'Dead (State 0)' : result_counts['0'], 'Alive (State 1)' : result_counts['1']}, legend=schrodinger_cat_experiment_legend, color='Orange', title=schrodinger_cat_experiment_title)

axis = schrodinger_cat_experiment.axes[0]
axis.set_ylim(0, 1)

schrodinger_cat_experiment.savefig('imgs/schrodinger-cat-experiment-simple-random.png', bbox_inches='tight')

schrodinger_cat_experiment<jupyter_output>Data Results from the Schrödinger's Cat Experiment:
- Dead (State 0): 484;
- Alive (State 1): 516;
{'fraction_non_alphanumeric': 0.12402028584601199, 'fraction_numerical': 0.007607192254495159, 'mean_word_length': 6.3846808510638295, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 28, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '258029', 'license': 'MIT', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2655, 'n_words': 750, 'path': 'tutorials/qiskit/02. fundamentals/b. schrodinger-cat-experiment/.ipynb_checkpoints/schrodinger-cat-experiment-simple-and-random-experiment-checkpoint.ipynb', 'repo_name': 'rubenandrebarreiro/learning-quantum-computing-course', 'size': '8690'}