quantum-algorithms

git clone git://git.codymlewis.com/quantum-algorithms.git
Log | Files | Refs | README

fanout (2351B)


      1 #!/usr/bin/env python3
      2 
      3 '''
      4 A simple quantum circuit that replicates the state of a qubit to N others
      5 
      6 Copyright (C) 2020 Cody Lewis
      7 
      8 This program is free software: you can redistribute it and/or modify
      9 it under the terms of the GNU General Public License as published by
     10 the Free Software Foundation, either version 3 of the License, or
     11 (at your option) any later version.
     12 
     13 This program is distributed in the hope that it will be useful,
     14 but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 GNU General Public License for more details.
     17 
     18 You should have received a copy of the GNU General Public License
     19 along with this program.  If not, see <https://www.gnu.org/licenses/>.
     20 '''
     21 
     22 import argparse
     23 
     24 from qiskit import QuantumCircuit, execute
     25 from qiskit.providers.aer import Aer
     26 
     27 def to_statevector(a):
     28     '''Convert a binary string into its representative qubit statevector'''
     29     if a == "0":
     30         return [1, 0]
     31     return [0, 1]
     32 
     33 def create_circuit(in_state, n):
     34     '''Construct the circuit with the input states set'''
     35     qcircuit = QuantumCircuit(n)
     36     qcircuit.initialize(to_statevector(in_state[0]), 0)
     37     for i in range(1, n):
     38         qcircuit.cnot(0, i)
     39     meas = QuantumCircuit(n, n)
     40     meas.barrier(range(n))
     41     meas.measure(range(n), range(n))
     42     return qcircuit + meas
     43 
     44 
     45 if __name__ == '__main__':
     46     print(
     47         "fanout  Copyright (C) 2020  Cody Lewis\n" +
     48         "This program comes with ABSOLUTELY NO WARRANTY.\n" +
     49         "This is free software, and you are welcome to redistribute it under certain conditions.\n"
     50     )
     51     PARSER = argparse.ArgumentParser(
     52         description="A simple quantum circuit that replicates the state of a qubit to N others"
     53     )
     54     PARSER.add_argument(
     55         'in_state', metavar='INPUT_STATE', type=str,
     56         help='Binary string representing the input state of the qubit'
     57     )
     58     PARSER.add_argument(
     59         'n', metavar='N', type=int,
     60         help='The number of qubits to fanout to'
     61     )
     62     ARGS = PARSER.parse_args()
     63     QCIRC = create_circuit(ARGS.in_state, ARGS.n)
     64     print("Created circuit:")
     65     print(QCIRC.draw())
     66     BACKEND_SIM = Aer.get_backend('qasm_simulator')
     67     RESULT = execute(QCIRC, BACKEND_SIM).result()
     68     print(f'Result: |{ARGS.in_state}> -> |{max(RESULT.get_counts())[::-1]}>')