Created
January 5, 2021 08:59
-
-
Save BankNatchapol/41dc66ad80e4d9db9d2fbea4801d947e to your computer and use it in GitHub Desktop.
Quantum XOR Gate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xor_gate(bit1, bit2, backend='sim', token=None): | |
assert bit1 in (0, 1) and bit2 in (0, 1), "Only 0 or 1" | |
quantumBits = 2 | |
classicalBits = 1 | |
XOR = QuantumCircuit(quantumBits, classicalBits) | |
if bit1 == 1: | |
XOR.x(0) | |
if bit2 == 1: | |
XOR.x(1) | |
XOR.barrier() | |
XOR.cx(0, 1) | |
XOR.barrier() | |
XOR.measure([1], [0]) | |
if backend == 'sim': | |
sim = Aer.get_backend('qasm_simulator') | |
result = execute(XOR, backend=sim, shots=1).result() | |
elif backend == 'real': | |
IBMQ.delete_account() | |
IBMQ.save_account(token, overwrite=True) | |
IBMQ.load_account() | |
# execute on real quantum computer | |
provider = IBMQ.get_provider('ibm-q') | |
qcomp = provider.get_backend('ibmq_16_melbourne') | |
job = execute(XOR, backend=qcomp) | |
result = job.result() # get result from real quantum computer | |
counts = result.get_counts() | |
value = max(counts, key=counts.get) | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment