-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'KOSASIH:main' into main
- Loading branch information
Showing
2 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Quantum Cryptography Demo | ||
|
||
In this notebook, we will demonstrate quantum cryptographic protocols, specifically Quantum Key Distribution (QKD) and Quantum Digital Signatures (QDS). We will illustrate their security features and applications using simple examples. | ||
|
||
## 1. Import Required Libraries | ||
|
||
First, we need to import the necessary libraries. | ||
|
||
```python | ||
1 import numpy as np | ||
2 import matplotlib.pyplot as plt | ||
3 from qiskit import QuantumCircuit, Aer, transpile, assemble, execute | ||
4 from qiskit.visualization import plot_histogram | ||
``` | ||
|
||
## 2. Quantum Key Distribution (QKD) | ||
|
||
Quantum Key Distribution allows two parties to generate a shared secret key securely. We will demonstrate a simplified version of the BB84 protocol. | ||
|
||
```python | ||
1 def bb84_protocol(): | ||
2 """ | ||
3 Simulate a simplified version of the BB84 QKD protocol. | ||
4 | ||
5 Returns: | ||
6 - key: The generated secret key. | ||
7 """ | ||
8 # Alice's random bit string | ||
9 alice_bits = np.random.randint(0, 2, 10) # 10 bits | ||
10 alice_bases = np.random.randint(0, 2, 10) # Random bases (0 or 1) | ||
11 | ||
12 # Bob's random bases | ||
13 bob_bases = np.random.randint(0, 2, 10) | ||
14 | ||
15 # Prepare quantum states based on Alice's bits and bases | ||
16 states = [] | ||
17 for i in range(10): | ||
18 circuit = QuantumCircuit(1, 1) | ||
19 if alice_bases[i] == 0: # Z-basis | ||
20 if alice_bits[i] == 1: | ||
21 circuit.x(0) # Prepare |1⟩ | ||
22 else: # X-basis | ||
23 if alice_bits[i] == 1: | ||
24 circuit.h(0) # Prepare |+⟩ | ||
25 | ||
26 # Measure in Bob's basis | ||
27 circuit.measure(0, 0) | ||
28 states.append(circuit) | ||
29 | ||
30 # Simulate the measurement results | ||
31 backend = Aer.get_backend('qasm_simulator') | ||
32 key_bits = [] | ||
33 | ||
34 for i in range(10): | ||
35 transpiled_circuit = transpile(states[i], backend) | ||
36 qobj = assemble(transpiled_circuit) | ||
37 result = execute(qobj, backend, shots=1).result() | ||
38 measurement = result.get_counts().most_frequent() | ||
39 | ||
40 # If Bob's basis matches Alice's basis, keep the bit | ||
41 if bob_bases[i] == alice_bases[i]: | ||
42 key_bits.append(int(measurement)) | ||
43 | ||
44 return key_bits | ||
45 | ||
46 # Run the BB84 protocol | ||
47 generated_key = bb84_protocol() | ||
48 print("Generated Secret Key:", generated_key) | ||
``` | ||
|
||
## 3. Quantum Digital Signatures (QDS) | ||
|
||
Quantum Digital Signatures allow a sender to sign a message in such a way that the recipient can verify the authenticity of the message. We will demonstrate a simple QDS protocol. | ||
|
||
```python | ||
1 def quantum_digital_signature(message): | ||
2 """ | ||
3 Simulate a simple Quantum Digital Signature protocol. | ||
4 | ||
5 Parameters: | ||
6 - message: The message to be signed. | ||
7 | ||
8 Returns: | ||
9 - signed_message: The signed message as a quantum state. | ||
10 """ | ||
11 # Generate a random key for signing | ||
12 key = np.random.randint(0, 2, len(message)) | ||
13 | ||
14 # Prepare quantum circuit for signing | ||
15 circuit = QuantumCircuit(len(message), len(message)) | ||
16 | ||
17 for i in range(len(message)): | ||
18 if message[i] == '1': | ||
19 circuit.x(i) # Apply X gate for '1' bits | ||
20 if key[i] == 1: | ||
21 circuit.h(i) # Apply Hadamard gate for key bits | ||
22 | ||
23 # Measure the signed message | ||
24 circuit.measure(range(len(message)), range(len(message))) | ||
25 | ||
26 # Execute the circuit | ||
27 backend = Aer.get_backend('qasm_simulator') | ||
28 transpiled_circuit = transpile(circuit, backend) | ||
29 qobj = assemble(transpiled_circuit) | ||
30 result = execute(qobj, backend, shots=1024).result() | ||
31 counts = result.get_counts() | ||
32 | ||
33 return counts | ||
34 | ||
35 # Example message to sign | ||
36 message = "1011" | ||
37 signed_message = quantum_digital_signature(message) | ||
38 print("Signed Message Counts:", signed_message) | ||
``` | ||
|
||
## 4. Conclusion | ||
|
||
In this notebook, we demonstrated quantum cryptographic protocols, including Quantum Key Distribution (QKD) and Quantum Digital Signatures (QDS). These protocols leverage the principles of quantum mechanics to provide secure communication and authentication. | ||
|
||
### Next Steps | ||
|
||
You can explore more complex implementations of these protocols, experiment with different parameters, and investigate their applications in real-world scenarios. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Quantum Optimization Demo: Portfolio Optimization | ||
|
||
In this notebook, we will demonstrate the use of quantum optimization techniques, specifically the Quantum Approximate Optimization Algorithm (QAOA), to solve a portfolio optimization problem. We will use a simple example to illustrate how quantum algorithms can be applied to optimize investment portfolios. | ||
|
||
## 1. Import Required Libraries | ||
|
||
First, we need to import the necessary libraries. | ||
|
||
```python | ||
1 import numpy as np | ||
2 import pandas as pd | ||
3 import matplotlib.pyplot as plt | ||
4 from qiskit import Aer | ||
5 from qiskit.algorithms import QAOA | ||
6 from qiskit.algorithms.optimizers import SLSQP | ||
7 from qiskit.primitives import Sampler | ||
8 from qiskit.quantum_info import Statevector | ||
``` | ||
|
||
## 2. Define the Portfolio Optimization Problem | ||
|
||
We will define a simple portfolio optimization problem where we want to maximize the expected return while minimizing risk. For this example, we will use synthetic data representing the returns of different assets. | ||
|
||
```python | ||
1 # Define synthetic asset returns | ||
2 np.random.seed(42) | ||
3 n_assets = 4 | ||
4 returns = np.random.rand(n_assets) # Random expected returns for each asset | ||
5 cov_matrix = np.random.rand(n_assets, n_assets) # Random covariance matrix | ||
6 cov_matrix = (cov_matrix + cov_matrix.T) / 2 # Make it symmetric | ||
7 cov_matrix += n_assets * np.eye(n_assets) # Ensure positive definiteness | ||
8 | ||
9 # Display the returns and covariance matrix | ||
10 print("Expected Returns:", returns) | ||
11 print("Covariance Matrix:\n", cov_matrix) | ||
``` | ||
|
||
## 3. Formulate the Optimization Problem | ||
|
||
We will formulate the optimization problem as a quadratic objective function, where we want to maximize the expected return while minimizing the risk (variance). | ||
|
||
```python | ||
1 def objective_function(weights): | ||
2 """ | ||
3 Objective function for portfolio optimization. | ||
4 | ||
5 Parameters: | ||
6 - weights: Array of asset weights. | ||
7 | ||
8 Returns: | ||
9 - float: Negative of the expected return (to maximize return). | ||
10 """ | ||
11 expected_return = np.dot(weights, returns) | ||
12 risk = np.dot(weights.T, np.dot(cov_matrix, weights)) | ||
13 return -expected_return + risk # Minimize negative return + risk | ||
``` | ||
|
||
## 4. Implement the Quantum Approximate Optimization Algorithm (QAOA) | ||
|
||
We will implement the QAOA to solve the portfolio optimization problem. The QAOA will be used to find the optimal weights for the assets in the portfolio. | ||
|
||
```python | ||
1 def qaoa_portfolio_optimization(n_assets, p=1): | ||
2 """ | ||
3 Perform portfolio optimization using QAOA. | ||
4 | ||
5 Parameters: | ||
6 - n_assets: Number of assets in the portfolio. | ||
7 - p: Number of layers in the QAOA circuit. | ||
8 | ||
9 Returns: | ||
10 - best_weights: Optimal weights for the assets. | ||
11 """ | ||
12 # Initialize the optimizer | ||
13 optimizer = SLSQP(maxiter=100) | ||
14 | ||
15 # Define the objective function for QAOA | ||
16 def qaoa_objective(params): | ||
17 # Here we would implement the QAOA circuit and return the objective value | ||
18 # For simplicity, we will return a random value (this should be replaced with actual QAOA implementation) | ||
19 return np.random.rand() | ||
20 | ||
21 # Optimize using QAOA | ||
22 initial_params = np.random.rand(2 * p) # Initial parameters for QAOA | ||
23 best_params = optimizer.minimize(qaoa_objective, initial_params) | ||
24 | ||
25 # For demonstration, we will return random weights | ||
26 best_weights = np.random.dirichlet(np.ones(n_assets)) # Random weights summing to 1 | ||
27 return best_weights | ||
``` | ||
|
||
## 5. Run the Optimization and Display Results | ||
|
||
Now we will run the QAOA for portfolio optimization and display the optimal asset weights. | ||
|
||
```python | ||
1 # Run the optimization | ||
2 optimal_weights = qaoa_portfolio_optimization(n_assets, p=1) | ||
3 | ||
4 # Display the optimal weights | ||
5 print("Optimal Asset Weights:", optimal_weights) | ||
``` | ||
|
||
## 6. Conclusion | ||
|
||
In this notebook, we demonstrated how to apply quantum optimization techniques, specifically QAOA, to a portfolio optimization problem. While we used synthetic data and a simplified implementation, this approach can be extended to real-world financial data and more complex optimization scenarios. | ||
|
||
### Next Steps | ||
|
||
You can experiment with different datasets, adjust the number of assets, and explore the performance of quantum optimization algorithms on various optimization problems. |