-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfidelity_count.py
More file actions
58 lines (41 loc) · 2.15 KB
/
Copy pathfidelity_count.py
File metadata and controls
58 lines (41 loc) · 2.15 KB
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
import perceval as pcvl
def calculate_fidelity(circuit, total_samples = 1000, print_results = False) :
"""Takes into argument a circuit, a number of samples desired and returns the fidelities
of the state 1 and state 2 as F1, F2"""
# Initialize the SLOS processor
processor = pcvl.Processor("SLOS", circuit)
processor.with_input(pcvl.BasicState([0, 1, 0, 1])) #the input state of the cloning circuit according to the research paper
# Create the sampler
sampler = pcvl.algorithm.Sampler(processor)
# Sample the output distribution (default 1000 samples)
sample_count = sampler.sample_count(total_samples)
# Initialize counts for target states
count_F1 = 0
count_F2 = 0
if print_results == True :
print("Sample Counts:", sample_count['results'])
# Iterate through sampled states, and add to count simultaneous detections between one photon exiting one of the output modes 1-2,
# and the other exiting one of the output modes 3-4
for state, count in sample_count['results'].items():
if state == pcvl.BasicState("|0,1,0,1>"): # Clone 1 (correct output for mode 1)
count_F1 = count
if state == pcvl.BasicState("|0,1,1,0>"): # Clone 1 (correct output for mode 1)
count_F1 += count
if state == pcvl.BasicState("|1,0,1,0>"): # Clone 2 (correct output for mode 2)
count_F2 = count
if state == pcvl.BasicState("|0,1,1,0>"): # Clone 2 (correct output for mode 2)
count_F2 += count
# Normalize to calculate fidelities
F1 = count_F1 / total_samples
F2 = count_F2 / total_samples
# print(f"Fidelity F1 (Clone 1): {F1}")
# print(f"Fidelity F2 (Clone 2): {F2}")
return(F1, F2)
## TO TEST THE FUNCTION
# phis = [0, np.pi/2, np.pi, 3*np.pi/2] # Values we need to train the variational cloning machine.
# # A random seed for reproducibility
# np.random.seed(157)
# # Define the circuit with the desired input parameters
# thetas = [np.random.uniform(0, 2 * np.pi) for _ in range(12)] # Example: random initial values
# circuit = create_cloning_circuit(phis[1], thetas)
# calculate_fidelity(circuit, print_results = True)