Skip to content

Commit

Permalink
#107 create avg_photon_num for full circuit state
Browse files Browse the repository at this point in the history
  • Loading branch information
tjstavenger-pnnl committed Feb 1, 2024
1 parent 7293282 commit c956077
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
26 changes: 22 additions & 4 deletions c2qa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,17 +713,35 @@ def fockmap(matrix, fock_input, fock_output, amplitude=[]):
raise ValueError("Please ensure that your args are correctly defined.")


def avg_photon_num(state, decimals: int=2):
"""Returns average photon number of state using the number operator.
def avg_photon_num(circuit: CVCircuit, state, decimals: int=2):
"""Returns average photon number of state for each qumode within the circuit using the number operator.
Args:
state (Statevector or DensityMatrix): State to operate on
circuit (CVCircuit): Circuit definine qumodes present in given state
state (Statevector or DensityMatrix): full state to operate on
decimals: Determines precision of calculation
Returns:
float: Average photon number to specified precision
"""
averages = []
for qumode_qubits in circuit.qumode_qubits_indices_grouped:
traced_state = qiskit.quantum_info.partial_trace(state, qumode_qubits)
averages.append(qumode_avg_photon_num(traced_state, decimals))

return averages

def qumode_avg_photon_num(state, decimals: int=2):
"""Returns average photon number of an individual qumode's state using the number operator.
Args:
state (Statevector or DensityMatrix): State to operate on for an individual qumode
decimals: Determines precision of calculation
Returns:
float: Average photon number to specified precision
"""

# Generate number operator based on dimension of state
dim = state.dim
N = np.diag(range(dim))
Expand All @@ -742,4 +760,4 @@ def avg_photon_num(state, decimals: int=2):
if round(avg_photon.imag, 6) != 0:
raise Exception("Magnitude of average photon is complex, check inputs. Imaginary portion = {}".format(avg_photon.imag))

return np.round(avg_photon.real, decimals)
return np.round(avg_photon.real, decimals)
4 changes: 2 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def test_counts_to_fockcounts(capsys):
assert(fock_counts == c2qa.util.cv_fockcounts(result.get_counts(), regs))


def test_avg_photon_num(capsys):
def test_qumode_avg_photon_num(capsys):
with capsys.disabled():
for _ in range(5): # Repeat test 5 times
# Decimals
Expand All @@ -284,6 +284,6 @@ def test_avg_photon_num(capsys):
avg_num = numpy.mean(numpy.dot(element_norm, numpy.array(range(dim))))/norm

# Average photon number of statevector, density matrix, and random vector must all match
assert(c2qa.util.avg_photon_num(Statevector(vector), decimals) == c2qa.util.avg_photon_num(DensityMatrix(vector), decimals) == round(avg_num.real, decimals))
assert(c2qa.util.qumode_avg_photon_num(Statevector(vector), decimals) == c2qa.util.qumode_avg_photon_num(DensityMatrix(vector), decimals) == round(avg_num.real, decimals))


4 changes: 2 additions & 2 deletions tutorials/bosonic-kitten-code/bosonic-kitten-code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@
" cycle_list_probB.append([fid0, fiderr]) # w/ respect to specific states\n",
" \n",
" # Avg photon num\n",
" #cycle_list_avg_photon.append(c2qa.util.avg_photon_num(matrix))\n",
" #cycle_list_avg_photon.append(c2qa.util.qumode_avg_photon_num(matrix))\n",
"\n",
" expectation_list.append(cycle_list_expectation)\n",
" exp_prob_list.append(cycle_list_exp_prob)\n",
Expand Down Expand Up @@ -2413,7 +2413,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c956077

Please sign in to comment.