Path: blob/main/translations/ja/ch-ex/ex1.ipynb
3855 views
Kernel: Python 3
量子回路による古典論理ゲートの実現
In [1]:
NOTゲート(Qiskitではxと表現)、CNOTゲート(Qiskitではcxと表現)、Toffoliゲート(Qiskitではccxと表現)を使用して、XOR、AND、NAND、および ORゲートを実装してください。
例として、NOTゲートの実装を示します。
NOT ゲート
この関数は、バイナリ文字列入力( '0'または' 1')を受け取り、反対のバイナリ出力 を返します。
In [2]:
XOR ゲート
2つのバイナリ文字列を入力として受け取り、1つのバイナリ文字列を出力します。
入力が等しい場合、出力は '0'であり、そうでない場合、出力は' 1'です。
In [3]:
AND ゲート
2つのバイナリ文字列を入力として受け取り、1つのバイナリ文字列を出力します。
両方の入力が '1'の場合のみ、出力は' 1'になります。
In [4]:
NAND ゲート
2つのバイナリ文字列を入力として受け取り、1つのバイナリ文字列を出力します。
両方の入力が '1'の場合のみ、出力は'0'になります。
In [5]:
OR ゲート
2つのバイナリ文字列を入力として受け取り、1つのバイナリ文字列を出力します。
いずれかの入力が '1'の場合、出力は'1'になります。
In [6]:
検証
次のコードを実行し、すべての可能な入力に対して上記の関数が機能するかどうかを確認してください。
In [7]:
Out[7]:
Results for the NOT gate
Input 0 gives output 1
Input 1 gives output 0
Results for the XOR gate
Inputs 0 0 give output 0
Inputs 0 1 give output 1
Inputs 1 0 give output 1
Inputs 1 1 give output 0
Results for the AND gate
Inputs 0 0 give output 0
Inputs 0 1 give output 0
Inputs 1 0 give output 0
Inputs 1 1 give output 1
Results for the NAND gate
Inputs 0 0 give output 1
Inputs 0 1 give output 1
Inputs 1 0 give output 1
Inputs 1 1 give output 0
Results for the OR gate
Inputs 0 0 give output 0
Inputs 0 1 give output 1
Inputs 1 0 give output 1
Inputs 1 1 give output 1
In [8]:
Out[8]:
{'qiskit-terra': '0.16.1',
'qiskit-aer': '0.7.1',
'qiskit-ignis': '0.5.1',
'qiskit-ibmq-provider': '0.11.1',
'qiskit-aqua': '0.8.1',
'qiskit': '0.23.1'}
翻訳担当: 野ヶ山尊秀 (nogayama @jp.ibm.com)