-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (59 loc) · 2.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from qiskit import QuantumCircuit, Aer, transpile, assemble
from qiskit_aer import AerSimulator
import hashlib
import time
class QuantumBlock:
def __init__(self, data):
self.data = data
self.previous_hash = None
self.hash = None
self.qc = self.create_quantum_circuit()
def create_quantum_circuit(self):
# Create a quantum circuit representing the data
qc = QuantumCircuit(len(self.data), 1)
for i, bit in enumerate(self.data):
if bit == '1':
qc.x(i)
return qc
def mine(self, difficulty):
# Mine the block by finding a hash with required difficulty
target = '0' * difficulty
nonce = 0
while True:
self.qc.reset(0)
self.qc.measure(0, 0)
backend = Aer.get_backend('qasm_simulator')
job = assemble(self.qc, shots=1)
result = AerSimulator().run(job).result()
counts = result.get_counts()
output_hash = hashlib.sha256(str(counts).encode()).hexdigest()
if output_hash.startswith(target):
self.hash = output_hash
break
nonce += 1
print("Block mined with nonce:", nonce)
class QuantumBlockchain:
def __init__(self):
self.chain = []
self.difficulty = 4 # Difficulty for PoW
def add_block(self, data):
if len(self.chain) > 0:
previous_hash = self.chain[-1].hash
else:
previous_hash = None
new_block = QuantumBlock(data)
new_block.previous_hash = previous_hash
new_block.mine(self.difficulty)
self.chain.append(new_block)
def print_chain(self):
for block in self.chain:
print("Data:", block.data)
print("Hash:", block.hash)
print("Previous Hash:", block.previous_hash)
print()
# Example usage
if __name__ == "__main__":
blockchain = QuantumBlockchain()
blockchain.add_block("Hello, quantum world!")
blockchain.add_block("Quantum computing rocks!")
blockchain.print_chain()