[PYTHON] Qiskit: I want to create a circuit that creates arbitrary states! !!

Introduction

I want to easily create a circuit that creates an arbitrary state! Do you have any thoughts? For example ...

python


\frac{|0>+|1>}{\sqrt{2}}

Well, this is easy. It can be created with the following circuit.

1.png

But there are tools that can create this state without knowing the Hadamard gate! That is the StateVectorCircuit.

StateVectorCircuit

Let's create a circuit that creates the above state using StateVectorCircuit.

First, import the package to be used.

python


import numpy as np
from qiskit.aqua.circuits import StateVectorCircuit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute
from qiskit import BasicAer
from qiskit.visualization import plot_histogram

Next, define the state you want to create this time

python


state = [1 / np.sqrt(2), 1 / np.sqrt(2)]

Enter this into the StateVectorCircuit.

python


svc = StateVectorCircuit(state)

Two values are stored here.

python


print(svc._num_qubits)
# 1
print(svc._state_vector)
# [0.70710678 0.70710678]

Since the circuit has not been created yet, construct_circuit is performed here.

qc = svc.construct_circuit()

This is the created circuit

2.png

Let's actually execute it and see the result.

cr = ClassicalRegister(1)
qc.add_register(cr)
qc.measure([0], [0])
num_shots = 10000
backend = BasicAer.get_backend('qasm_simulator')
results = execute(qc, backend, shots=num_shots).result()
counts = results.get_counts(qc)
plot_histogram(counts1)

3.png

HM. It feels good.

By the way, it will normalize without permission, so for example

python


state = [100, 100]

It will also support you. Of course, multiple qubits are fine.

Summary

This time, I introduced StateVectorCircuit, which seems to be useful. By the way, if the version of Qiskit you are using is old, you may get an error. Qiskit itself is evolving day by day.

Recommended Posts

Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to create a window in Python
I want to create a plug-in type implementation
I want to create a priority queue that can be updated in Python (2.7)
I want to create a web application that uses League of Legends data ①
I want to manually create a legend with matplotlib
I want to create a Dockerfile for the time being.
I tried to create a server environment that runs on Windows 10
I want to use a wildcard that I want to shell with Python remove
I want to create a system to prevent forgetting to tighten the key 1
I want to create a pipfile and reflect it in docker
I want to create a machine learning service without programming! WebAPI
I tried to create a linebot (implementation)
I want to print in a comprehension
I tried to create a linebot (preparation)
I want to build a Python environment
I want to identify the alert email. --Is that x a wildcard? ---
I want to create a machine learning service without programming! Text classification
I want to make matplotlib a dark theme
I want to INSERT a DataFrame into MSSQL
I want to make a game with Python
I don't want to take a coding test
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to upload a Django app to heroku
I want to create a nice Python development environment for my new Mac
I tried to create a class that can easily serialize Json in Python
I want to exe and distribute a program that resizes images Python3 + pyinstaller
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I tried to create a table only with Django
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
I want to write in Python! (2) Let's write a test
The story of Linux that I want to teach myself half a year ago
[Mac] I want to make a simple HTTP server that runs CGI with Python
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I want to work with a robot in python.
I want to split a character string with hiragana
I want to install a package of Php Redis
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
[Python] I want to make a nested list a tuple
A story that I was addicted to at np.where
I want to send a business start email automatically
I want to say that there is data preprocessing ~
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I made a tool that makes it a little easier to create and install a public key.
I want to find a stock that will rise 5 minutes after the Nikkei Stock Average rises
I want to create a karaoke sound source by separating instruments and vocals using Python
[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.
Create a plugin that always highlights arbitrary text in Sublime Text 2