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 simples (pode ser substituído por SHA-256)
# ============================================================
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. Modelo de ruído escalável
# ============================================================
def scalable_noise(scale):
noise = NoiseModel()
# Parâmetros base (podem ser substituídos por IBM Heron)
T1 = 100e-6 / scale
T2 = 80e-6 / scale
gate_time = 50e-9
thermal = thermal_relaxation_error(T1, T2, gate_time)
dep1 = depolarizing_error(0.001 * scale, 1)
dep2 = depolarizing_error(0.01 * scale, 2)
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 = scalable_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_noise_decode(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_noise_decode()
import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.providers.aer.noise import NoiseModel, thermal_relaxation_error, depolarizing_error
# ============================================================
# 1. Ruído de altitude
# ============================================================
def noise_altitude(temp_celsius):
base_T1 = 80e-6
base_T2 = 60e-6
factor = max(0.2, min(2.0, (25 - temp_celsius) / 25))
T1 = base_T1 * factor
T2 = base_T2 * factor
noise = NoiseModel()
thermal = thermal_relaxation_error(T1, T2, 50e-9)
em = depolarizing_error(0.02, 1)
vib = depolarizing_error(0.01, 1)
noise.add_all_qubit_quantum_error(thermal, ['x','h'])
noise.add_all_qubit_quantum_error(em, ['cx'])
noise.add_all_qubit_quantum_error(vib, ['h'])
return noise
# ============================================================
# 2. SHA-256 reversível ideal (placeholder)
# ============================================================
def SHA256_reversible(qc, x_qubits, h_qubits):
qc.cx(x_qubits, h_qubits)
# ============================================================
# 3. Oracle SHA-256
# ============================================================
def oracle_SHA256(qc, x_qubits, h_qubits, target_hash):
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
qc.mcx(h_qubits[:-1], h_qubits[-1])
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# ============================================================
# 4. Difusão
# ============================================================
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 + ruído de altitude
# ============================================================
def grover_SHA256_altitude(n=8, target_hash="10110011", iterations=2, temp=-60):
qc = QuantumCircuit(2*n, n)
x = list(range(n))
h = list(range(n, 2*n))
qc.h(x)
SHA256_reversible(qc, x, h)
for _ in range(iterations):
oracle_SHA256(qc, x, h, target_hash)
diffusion(qc, x)
qc.measure(x, range(n))
backend = Aer.get_backend("qasm_simulator")
noise = noise_altitude(temp)
result = execute(qc, backend, noise_model=noise, shots=4096).result()
return result.get_counts()
# ============================================================
# 6. Decode sob ruído
# ============================================================
def decode_noisy(counts):
return max(counts, key=counts.get)
# ============================================================
# 7. Execução
# ============================================================
counts = grover_SHA256_altitude()
print("Counts:", counts)
print("Pré-imagem encontrada:", decode_noisy(counts))
from qiskit.providers.aer.noise import NoiseModel, thermal_relaxation_error, depolarizing_error
def noise_ibm_heron():
noise = NoiseModel()
# Coerência realista
T1 = 120e-6
T2 = 90e-6
gate_time = 80e-9
# Erros realistas
err_1q = 0.0005
err_2q = 0.005
thermal = thermal_relaxation_error(T1, T2, gate_time)
dep1 = depolarizing_error(err_1q, 1)
dep2 = depolarizing_error(err_2q, 2)
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
def ZNE(qc, noise_fn):
backend = Aer.get_backend("qasm_simulator")
scales = [0.5, 1, 2, 3]
results = []
for s in scales:
noise = noise_fn(s)
job = execute(qc, backend, noise_model=noise, shots=4096)
results.append(job.result().get_counts())
# Extrapolação linear simples
probs = [res.get(max(res, key=res.get), 0)/4096 for res in results]
zne_estimate = probs[0] + (probs[0] - probs[1]) # extrapolação linear
return zne_estimate
def PEC_correct(counts, error_rate=0.005):
corrected = {}
for state, c in counts.items():
corrected[state] = c * (1 + error_rate)
return corrected
def CDR_predict(counts):
# Regressão linear conceptual
total = sum(counts.values())
probs = {k: v/total for k, v in counts.items()}
return max(probs, key=probs.get)
def grover_SHA256_ibm(n=8, target_hash="10110011", iterations=2):
qc = QuantumCircuit(2*n, n)
x = list(range(n))
h = list(range(n, 2*n))
qc.h(x)
SHA256_reversible(qc, x, h)
for _ in range(iterations):
oracle_SHA256(qc, x, h, target_hash)
diffusion(qc, x)
qc.measure(x, range(n))
return qc
# Execução
qc = grover_SHA256_ibm()
backend = Aer.get_backend("qasm_simulator")
noise = noise_ibm_heron()
result = execute(qc, backend, noise_model=noise, shots=4096).result()
counts = result.get_counts()
# Mitigação
zne = ZNE(qc, lambda s: noise_ibm_heron())
pec = PEC_correct(counts)
cdr = CDR_predict(counts)
print("Counts brutos:", counts)
print("ZNE:", zne)
print("PEC:", pec)
print("CDR:", cdr)
from qiskit import QuantumCircuit, Aer, execute
# ============================================================
# 1. SHA-256 reversível ideal (abstração)
# ============================================================
def SHA256_reversible(qc, x_qubits, h_qubits):
# Representação ideal: copia x → h
# Em hardware real isto seria o circuito completo SHA-256 reversível
qc.cx(x_qubits, h_qubits)
# ============================================================
# 2. Oracle ideal para SHA-256
# ============================================================
def oracle_SHA256(qc, x_qubits, h_qubits, target_hash):
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
qc.mcx(h_qubits[:-1], h_qubits[-1])
for i, bit in enumerate(target_hash):
if bit == "0":
qc.x(h_qubits[i])
# ============================================================
# 3. Difusão ideal
# ============================================================
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 ideal para SHA-256
# ============================================================
def grover_SHA256(n=256, target_hash="0"*256, iterations=2):
qc = QuantumCircuit(2*n, n)
x = list(range(n))
h = list(range(n, 2*n))
qc.h(x)
SHA256_reversible(qc, x, h)
for _ in range(iterations):
oracle_SHA256(qc, x, h, target_hash)
diffusion(qc, x)
qc.measure(x, range(n))
return qc
# ============================================================
# 5. Decode ideal
# ============================================================
def decode(counts):
return max(counts, key=counts.get)
# ============================================================
# 6. Execução ideal
# ============================================================
backend = Aer.get_backend("qasm_simulator")
qc = grover_SHA256(n=8, target_hash="10110011") # versão reduzida para teste
result = execute(qc, backend, shots=4096).result()
counts = result.get_counts()
print("Counts:", counts)
print("Pré-imagem encontrada:", decode(counts))