Created
October 29, 2025 22:14
-
-
Save lennon03/f62b6dc7205e97105d28ea346ef0f8d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from typing import Dict | |
| from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister | |
| from qiskit_aer import AerSimulator | |
| # Mapping from operation code to symbol and function | |
| OPS: Dict[int, str] = {1: "+", 2: "-", 3: "*", 4: "/"} | |
| ALIASES: Dict[str, int] = { | |
| "1": 1, | |
| "+": 1, | |
| "add": 1, | |
| "addition": 1, | |
| "2": 2, | |
| "-": 2, | |
| "sub": 2, | |
| "subtract": 2, | |
| "subtraction": 2, | |
| "3": 3, | |
| "*": 3, | |
| "mul": 3, | |
| "multi": 3, | |
| "multiply": 3, | |
| "multiplication": 3, | |
| "4": 4, | |
| "/": 4, | |
| "div": 4, | |
| "divide": 4, | |
| "division": 4, | |
| "0": 0, | |
| "q": 0, | |
| "quit": 0, | |
| "exit": 0, | |
| } | |
| def qubit_addition(x: int, y: int, num_bits: int = 8) -> int: | |
| """Quantum addition using Qiskit ripple-carry adder.""" | |
| qr_x = QuantumRegister(num_bits, "x") | |
| qr_y = QuantumRegister(num_bits, "y") | |
| qr_carry = QuantumRegister(num_bits, "carry") | |
| cr = ClassicalRegister(num_bits + 1, "result") | |
| qc = QuantumCircuit(qr_x, qr_y, qr_carry, cr) | |
| # Encode x and y | |
| for i in range(num_bits): | |
| if x & (1 << i): | |
| qc.x(qr_x[i]) | |
| if y & (1 << i): | |
| qc.x(qr_y[i]) | |
| # Ripple-carry adder | |
| for i in range(num_bits): | |
| qc.cx(qr_x[i], qr_y[i]) | |
| qc.cx(qr_x[i], qr_carry[i]) | |
| qc.ccx(qr_y[i], qr_carry[i], qr_x[i]) | |
| if i < num_bits - 1: | |
| qc.cx(qr_x[i], qr_y[i]) | |
| qc.ccx(qr_y[i], qr_carry[i], qr_carry[i + 1]) | |
| qc.cx(qr_x[i], qr_y[i]) | |
| # Measure | |
| for i in range(num_bits): | |
| qc.measure(qr_y[i], cr[i]) | |
| qc.measure(qr_carry[num_bits - 1], cr[num_bits]) | |
| simulator = AerSimulator() | |
| result = simulator.run(qc, shots=1).result() | |
| counts = result.get_counts() | |
| return int(list(counts.keys())[0], 2) | |
| def qubit_subtraction(x: int, y: int, num_bits: int = 8) -> int: | |
| """Quantum subtraction using two's complement.""" | |
| if y == 0: | |
| return x | |
| max_val = 2**num_bits | |
| y_complement = (max_val - y) % max_val | |
| result = qubit_addition(x, y_complement, num_bits) % max_val | |
| return result | |
| def qubit_multiplication(x: int, y: int, num_bits: int = 8) -> int: | |
| """Quantum multiplication using shift-and-add.""" | |
| qr_x = QuantumRegister(num_bits, "x") | |
| qr_y = QuantumRegister(num_bits, "y") | |
| qr_result = QuantumRegister(num_bits * 2, "result") | |
| cr = ClassicalRegister(num_bits * 2, "c") | |
| qc = QuantumCircuit(qr_x, qr_y, qr_result, cr) | |
| # Encode x and y | |
| for i in range(num_bits): | |
| if x & (1 << i): | |
| qc.x(qr_x[i]) | |
| if y & (1 << i): | |
| qc.x(qr_y[i]) | |
| # Multiply using controlled operations | |
| for i in range(num_bits): | |
| for j in range(num_bits): | |
| if i + j < num_bits * 2: | |
| qc.ccx(qr_x[i], qr_y[j], qr_result[i + j]) | |
| qc.measure(qr_result, cr) | |
| simulator = AerSimulator() | |
| result = simulator.run(qc, shots=1).result() | |
| counts = result.get_counts() | |
| return int(list(counts.keys())[0], 2) | |
| def qubit_division(x: int, y: int, num_bits: int = 8) -> tuple: | |
| """Quantum division using restoring division algorithm.""" | |
| if y == 0: | |
| raise ValueError("Division by zero") | |
| quotient = 0 | |
| remainder = x | |
| for i in range(num_bits - 1, -1, -1): | |
| divisor_shifted = y << i | |
| if remainder >= divisor_shifted: | |
| remainder = qubit_subtraction( | |
| remainder, divisor_shifted, num_bits + i + 1 | |
| ) | |
| quotient |= 1 << i | |
| return quotient, remainder | |
| def prompt_operation() -> int: | |
| while True: | |
| print("\nSelect operation:") | |
| print(" 1) Addition (+)") | |
| print(" 2) Subtraction (-)") | |
| print(" 3) Multiplication (*)") | |
| print(" 4) Division (/)") | |
| print(" 0) Exit") | |
| choice = input("Choice (1/2/3/4 or 0 to exit): ").strip().lower() | |
| if choice in ALIASES: | |
| return ALIASES[choice] | |
| print("Invalid choice. Try again (e.g., 1, add, +, etc.).") | |
| def prompt_number(prompt: str) -> float: | |
| while True: | |
| raw = input(prompt).strip() | |
| try: | |
| return float(raw) | |
| except ValueError: | |
| print("Invalid number. Please enter a valid numeric value.") | |
| def compute(op: int, x: float, y: float) -> float: | |
| """Compute using quantum circuits (converts to integers).""" | |
| # Convert to integers for quantum operations | |
| x_int = int(abs(x)) | |
| y_int = int(abs(y)) | |
| # Determine if result should be negative | |
| x_negative = x < 0 | |
| y_negative = y < 0 | |
| if op == 1: # Addition | |
| result = qubit_addition(x_int, y_int) | |
| if x_negative and y_negative: | |
| result = -result | |
| elif x_negative: | |
| result = qubit_subtraction(y_int, x_int) | |
| elif y_negative: | |
| result = qubit_subtraction(x_int, y_int) | |
| return float(result) | |
| if op == 2: # Subtraction | |
| if x_negative and y_negative: | |
| result = qubit_subtraction(y_int, x_int) | |
| elif x_negative: | |
| result = -(qubit_addition(x_int, y_int)) | |
| elif y_negative: | |
| result = qubit_addition(x_int, y_int) | |
| else: | |
| result = qubit_subtraction(x_int, y_int) | |
| return float(result) | |
| if op == 3: # Multiplication | |
| result = qubit_multiplication(x_int, y_int) | |
| if x_negative != y_negative: | |
| result = -result | |
| return float(result) | |
| if op == 4: # Division | |
| quotient, remainder = qubit_division(x_int, y_int) | |
| result = float(quotient) + (float(remainder) / float(y_int)) | |
| if x_negative != y_negative: | |
| result = -result | |
| return result | |
| raise ValueError("Unsupported operation code.") | |
| def main() -> None: | |
| print("Quantum-Enhanced CLI Calculator (Qiskit)") | |
| print("Using quantum circuits for arithmetic operations!\n") | |
| while True: | |
| op = prompt_operation() | |
| if op == 0: | |
| print("Goodbye!") | |
| break | |
| x = prompt_number("Enter x: ") | |
| y = prompt_number("Enter y: ") | |
| if op == 4 and y == 0.0: | |
| print("Error: Division by zero is undefined. Try again.") | |
| continue | |
| try: | |
| result = compute(op, x, y) | |
| symbol = OPS[op] | |
| print(f"Result: {x} {symbol} {y} = {result}") | |
| except Exception as e: | |
| print(f"Error during quantum computation: {e}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment