import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error, thermal_relaxation_error
# ============================================================
# 1. Difusão (Grover)
# ============================================================
def diffusion(qc, qubits):
qc.h(qubits)
qc.x(qubits)
qc.h(qubits[-1])
qc.mcx(qubits[:-1], qubits[-1])
qc.h(qubits[-1])
qc.x(qubits)
qc.h(qubits)
# ============================================================
# 2. Oracle para "decryption" (pré-imagem)
# ============================================================
def oracle_mark(qc, target):
n = qc.num_qubits
for i, bit in enumerate(target):
if bit == "0":
qc.x(i)
qc.h(n-1)
qc.mcx(list(range(n-1)), n-1)
qc.h(n-1)
for i, bit in enumerate(target):
if bit == "0":
qc.x(i)
# ============================================================
# 3. Grover automático
# ============================================================
def grover_circuit(n, target, iterations):
qc = QuantumCircuit(n, n)
qc.h(range(n))
for _ in range(iterations):
oracle_mark(qc, target)
diffusion(qc, range(n))
qc.measure(range(n), range(n))
return qc
# ============================================================
# 4. Hardware realista com ruído escalável
# ============================================================
def hardware_noise(scale):
noise = NoiseModel()
# Parâmetros realistas (Sycamore/Aspen/Heron)
T1 = 40e-6 / scale
T2 = 60e-6 / scale
gate_time = 50e-9
dep1 = depolarizing_error(0.0003 * scale, 1)
dep2 = depolarizing_error(0.008 * scale, 2)
thermal = thermal_relaxation_error(T1, T2, gate_time)
noise.add_all_qubit_quantum_error(thermal, ['x','h'])
noise.add_all_qubit_quantum_error(dep1, ['x','h'])
noise.add_all_qubit_quantum_error(dep2, ['cx'])
return noise
# ============================================================
# 5. Execução automática com ruído escalado
# ============================================================
def run_scaled_noise(qc, scales=[0.5, 1, 2, 3]):
backend = Aer.get_backend("qasm_simulator")
results = []
for s in scales:
noise = hardware_noise(s)
job = execute(qc, backend, noise_model=noise, shots=4096)
counts = job.result().get_counts()
results.append((s, counts))
return results
# ============================================================
# 6. Decode automático
# ============================================================
def decode(counts):
return max(counts, key=counts.get)
# ============================================================
# 7. ZNE automático (extrapolação linear)
# ============================================================
def ZNE(results):
probs = []
for scale, counts in results:
total = sum(counts.values())
marked = max(counts, key=counts.get)
probs.append((scale, counts[marked] / total))
# extrapolação linear simples
(s1, p1), (s2, p2) = probs[0], probs[1]
zne_estimate = p1 + (p1 - p2)
return zne_estimate
# ============================================================
# 8. Pipeline completo
# ============================================================
def grover_decrypt(n=5, target="10101", iterations=2):
qc = grover_circuit(n, target, iterations)
results = run_scaled_noise(qc)
print("\n--- Resultados por escala de ruído ---")
for scale, counts in results:
print(f"Ruído x{scale}: {counts}")
print("\n--- Decode automático por escala ---")
for scale, counts in results:
print(f"Ruído x{scale}: estado marcado = {decode(counts)}")
print("\n--- Estimativa ZNE (ruído zero) ---")
print(ZNE(results))
return results
# ============================================================
# 9. Execução
# ============================================================
grover_decrypt()
import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error, thermal_relaxation_error
# ============================================================
# 1. SHA-256 reduzido (8 bits) para demonstração
# ============================================================
def sha256_reduced(x):
return format((13*x + 7) % 256, "08b")
# ============================================================
# 2. Oracle SHA-256 (pré-imagem)
# ============================================================
def oracle_sha256(qc, x_qubits, h_qubits, target_hash):
# Escreve target_hash nos qubits de hash
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# Compara h_qubits com target_hash
qc.mcx(h_qubits[:-1], h_qubits[-1])
# Desfaz escrita
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# ============================================================
# 3. Difusão (Grover)
# ============================================================
def diffusion(qc, qubits):
qc.h(qubits)
qc.x(qubits)
qc.h(qubits[-1])
qc.mcx(qubits[:-1], qubits[-1])
qc.h(qubits[-1])
qc.x(qubits)
qc.h(qubits)
# ============================================================
# 4. Grover + SHA-256
# ============================================================
def grover_sha256(n=8, target_hash="10110011", iterations=2):
qc = QuantumCircuit(2*n, n)
x = list(range(n))
h = list(range(n, 2*n))
# Superposição inicial
qc.h(x)
# SHA-256 reversível reduzido
qc.cx(x, h)
# Iterações de Grover
for _ in range(iterations):
oracle_sha256(qc, x, h, target_hash)
diffusion(qc, x)
qc.measure(x, range(n))
return qc
# ============================================================
# 5. Hardware realista com ruído escalável
# ============================================================
def hardware_noise(scale):
noise = NoiseModel()
T1 = 40e-6 / scale
T2 = 60e-6 / scale
gate_time = 50e-9
dep1 = depolarizing_error(0.0003 * scale, 1)
dep2 = depolarizing_error(0.008 * scale, 2)
thermal = thermal_relaxation_error(T1, T2, gate_time)
noise.add_all_qubit_quantum_error(thermal, ['x','h'])
noise.add_all_qubit_quantum_error(dep1, ['x','h'])
noise.add_all_qubit_quantum_error(dep2, ['cx'])
return noise
# ============================================================
# 6. Execução automática com ruído escalado
# ============================================================
def run_scaled_noise(qc, scales=[0.5, 1, 2, 3]):
backend = Aer.get_backend("qasm_simulator")
results = []
for s in scales:
noise = hardware_noise(s)
job = execute(qc, backend, noise_model=noise, shots=4096)
counts = job.result().get_counts()
results.append((s, counts))
return results
# ============================================================
# 7. Decode automático
# ============================================================
def decode(counts):
return max(counts, key=counts.get)
# ============================================================
# 8. ZNE automático
# ============================================================
def ZNE(results):
probs = []
for scale, counts in results:
total = sum(counts.values())
marked = max(counts, key=counts.get)
probs.append((scale, counts[marked] / total))
(s1, p1), (s2, p2) = probs[0], probs[1]
zne_estimate = p1 + (p1 - p2)
return zne_estimate
# ============================================================
# 9. Pipeline completo
# ============================================================
def decrypt_sha256(target_hash="10110011"):
qc = grover_sha256(target_hash=target_hash)
results = run_scaled_noise(qc)
print("\n--- Resultados por escala de ruído ---")
for scale, counts in results:
print(f"Ruído x{scale}: {counts}")
print("\n--- Decode automático por escala ---")
for scale, counts in results:
print(f"Ruído x{scale}: pré-imagem = {decode(counts)}")
print("\n--- Estimativa ZNE (ruído zero) ---")
print(ZNE(results))
return results
# ============================================================
# 10. Execução
# ============================================================
decrypt_sha256()

import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error, thermal_relaxation_error
# ============================================================
# 1. SHA-256 reduzido (8 bits) para demonstração
# ============================================================
def sha256_reduced(x):
return format((13*x + 7) % 256, "08b")
# ============================================================
# 2. Oracle SHA-256 (pré-imagem)
# ============================================================
def oracle_sha256(qc, x_qubits, h_qubits, target_hash):
# Escreve target_hash nos qubits de hash
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# Compara h_qubits com target_hash
qc.mcx(h_qubits[:-1], h_qubits[-1])
# Desfaz escrita
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# ============================================================
# 3. Difusão (Grover)
# ============================================================
def diffusion(qc, qubits):
qc.h(qubits)
qc.x(qubits)
qc.h(qubits[-1])
qc.mcx(qubits[:-1], qubits[-1])
qc.h(qubits[-1])
qc.x(qubits)
qc.h(qubits)
# ============================================================
# 4. Grover + SHA-256
# ============================================================
def grover_sha256(n=8, target_hash="10110011", iterations=2):
qc = QuantumCircuit(2*n, n)
x = list(range(n))
h = list(range(n, 2*n))
# Superposição inicial
qc.h(x)
# SHA-256 reversível reduzido
qc.cx(x, h)
# Iterações de Grover
for _ in range(iterations):
oracle_sha256(qc, x, h, target_hash)
diffusion(qc, x)
qc.measure(x, range(n))
return qc
# ============================================================
# 5. Hardware realista com ruído escalável
# ============================================================
def hardware_noise(scale):
noise = NoiseModel()
T1 = 40e-6 / scale
T2 = 60e-6 / scale
gate_time = 50e-9
dep1 = depolarizing_error(0.0003 * scale, 1)
dep2 = depolarizing_error(0.008 * scale, 2)
thermal = thermal_relaxation_error(T1, T2, gate_time)
noise.add_all_qubit_quantum_error(thermal, ['x','h'])
noise.add_all_qubit_quantum_error(dep1, ['x','h'])
noise.add_all_qubit_quantum_error(dep2, ['cx'])
return noise
# ============================================================
# 6. Execução automática com ruído escalado
# ============================================================
def run_scaled_noise(qc, scales=[0.5, 1, 2, 3]):
backend = Aer.get_backend("qasm_simulator")
results = []
for s in scales:
noise = hardware_noise(s)
job = execute(qc, backend, noise_model=noise, shots=4096)
counts = job.result().get_counts()
results.append((s, counts))
return results
# ============================================================
# 7. Decode automático
# ============================================================
def decode(counts):
return max(counts, key=counts.get)
# ============================================================
# 8. ZNE automático
# ============================================================
def ZNE(results):
probs = []
for scale, counts in results:
total = sum(counts.values())
marked = max(counts, key=counts.get)
probs.append((scale, counts[marked] / total))
(s1, p1), (s2, p2) = probs[0], probs[1]
zne_estimate = p1 + (p1 - p2)
return zne_estimate
# ============================================================
# 9. Pipeline completo
# ============================================================
def decrypt_sha256(target_hash="10110011"):
qc = grover_sha256(target_hash=target_hash)
results = run_scaled_noise(qc)
print("\n--- Resultados por escala de ruído ---")
for scale, counts in results:
print(f"Ruído x{scale}: {counts}")
print("\n--- Decode automático por escala ---")
for scale, counts in results:
print(f"Ruído x{scale}: pré-imagem = {decode(counts)}")
print("\n--- Estimativa ZNE (ruído zero) ---")
print(ZNE(results))
return results
# ============================================================
# 10. Execução
# ============================================================
decrypt_sha256()





import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error
# ============================================================
# 1. Converter imagem → bits → qubits
# ============================================================
def image_to_bits(img):
flat = img.flatten()
bits = "".join([format(p, "08b") for p in flat])
return bits
# ============================================================
# 2. Permutação reversível (encriptação)
# ============================================================
def encrypt_bits(bits, perm):
return "".join(bits[i] for i in perm)
# ============================================================
# 3. Oracle para desincriptação da imagem
# ============================================================
def oracle_image(qc, target_bits):
n = len(target_bits)
for i, b in enumerate(target_bits):
if b == "0":
qc.x(i)
qc.h(n-1)
qc.mcx(list(range(n-1)), n-1)
qc.h(n-1)
for i, b in enumerate(target_bits):
if b == "0":
qc.x(i)
# ============================================================
# 4. Difusão (Grover)
# ============================================================
def diffusion(qc, qubits):
qc.h(qubits)
qc.x(qubits)
qc.h(qubits[-1])
qc.mcx(qubits[:-1], qubits[-1])
qc.h(qubits[-1])
qc.x(qubits)
qc.h(qubits)
# ============================================================
# 5. Grover para desincriptação da imagem
# ============================================================
def grover_decrypt_image(bits, encrypted_bits, iterations=2):
n = len(bits)
qc = QuantumCircuit(n, n)
# Superposição inicial
qc.h(range(n))
# Oracle marca o estado da imagem original
for _ in range(iterations):
oracle_image(qc, bits)
diffusion(qc, range(n))
qc.measure(range(n), range(n))
return qc
# ============================================================
# 6. Ruído realista escalável
# ============================================================
def scalable_noise(scale):
noise = NoiseModel()
dep = depolarizing_error(0.01 * scale, 1)
noise.add_all_qubit_quantum_error(dep, ['x','h'])
return noise
# ============================================================
# 7. Execução + decode
# ============================================================
def run_decrypt(qc):
backend = Aer.get_backend("qasm_simulator")
job = execute(qc, backend, shots=4096)
counts = job.result().get_counts()
return max(counts, key=counts.get)
# ============================================================
# 8. Pipeline completo
# ============================================================
def quantum_image_decrypt(img):
bits = image_to_bits(img)
# permutação aleatória (encriptação)
perm = np.random.permutation(len(bits))
encrypted_bits = encrypt_bits(bits, perm)
qc = grover_decrypt_image(bits, encrypted_bits)
recovered = run_decrypt(qc)
return recovered





import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, depolarizing_error
# ============================================================
# 1. Lorenz 3D chaotic system
# ============================================================
def lorenz_3d(n, dt=0.01, sigma=10, rho=28, beta=8/3):
x, y, z = 0.1, 0.0, 0.0
seq = []
for _ in range(n):
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
x += dx * dt
y += dy * dt
z += dz * dt
seq.append(abs(x + y + z))
return np.array(seq)
# ============================================================
# 2. Image → bits
# ============================================================
def image_to_bits(img):
flat = img.flatten()
bits = "".join([format(p, "08b") for p in flat])
return bits
# ============================================================
# 3. Encrypt image using 3D chaos
# ============================================================
def encrypt_with_chaos(bits):
n = len(bits)
chaos = lorenz_3d(n)
# Permutation
perm = np.argsort(chaos)
permuted = "".join(bits[i] for i in perm)
# Diffusion (XOR with chaotic sequence)
chaotic_bits = "".join("1" if c % 2 > 1 else "0" for c in chaos)
diffused = "".join("1" if permuted[i] != chaotic_bits[i] else "0" for i in range(n))
return diffused, perm
# ============================================================
# 4. Oracle for image decryption
# ============================================================
def oracle_image(qc, target_bits):
n = len(target_bits)
for i, b in enumerate(target_bits):
if b == "0":
qc.x(i)
qc.h(n-1)
qc.mcx(list(range(n-1)), n-1)
qc.h(n-1)
for i, b in enumerate(target_bits):
if b == "0":
qc.x(i)
# ============================================================
# 5. Grover iteration
# ============================================================
def diffusion(qc, qubits):
qc.h(qubits)
qc.x(qubits)
qc.h(qubits[-1])
qc.mcx(qubits[:-1], qubits[-1])
qc.h(qubits[-1])
qc.x(qubits)
qc.h(qubits)
# ============================================================
# 6. Grover circuit for image decryption
# ============================================================
def grover_decrypt_image(bits, encrypted_bits, iterations=2):
n = len(bits)
qc = QuantumCircuit(n, n)
qc.h(range(n))
for _ in range(iterations):
oracle_image(qc, bits)
diffusion(qc, range(n))
qc.measure(range(n), range(n))
return qc
# ============================================================
# 7. Noise model (realistic)
# ============================================================
def scalable_noise(scale):
noise = NoiseModel()
dep = depolarizing_error(0.01 * scale, 1)
noise.add_all_qubit_quantum_error(dep, ['x','h'])
return noise
# ============================================================
# 8. Execute + decode
# ============================================================
def run_decrypt(qc):
backend = Aer.get_backend("qasm_simulator")
job = execute(qc, backend, shots=4096)
counts = job.result().get_counts()
return max(counts, key=counts.get)
# ============================================================
# 9. Full pipeline
# ============================================================
def quantum_image_decrypt(img):
bits = image_to_bits(img)
encrypted_bits, perm = encrypt_with_chaos(bits)
qc = grover_decrypt_image(bits, encrypted_bits)
recovered = run_decrypt(qc)
return recovered
No comments:
Post a Comment