Skip to content

Commit

Permalink
updated projector method in sym.base
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisRalli committed Oct 17, 2022
1 parent 31afc28 commit 8248d2c
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 1 deletion.
179 changes: 179 additions & 0 deletions notebooks/draft/PauliwordOp projector.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c72945b3",
"metadata": {},
"outputs": [],
"source": [
"from symmer.symplectic import get_PauliwordOp_projector, PauliwordOp, QuantumState"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0de88654",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.033-0.108j |000> +\n",
"-0.127-0.254j |001> +\n",
"-0.365-0.027j |010> +\n",
" 0.248-0.218j |011> +\n",
" 0.566+0.239j |100> +\n",
"-0.456-0.127j |101> +\n",
"-0.143+0.155j |110> +\n",
"-0.131+0.014j |111>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test = QuantumState.haar_random(3, vec_type='ket')\n",
"test"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "59946fa4",
"metadata": {},
"outputs": [],
"source": [
"proj = get_PauliwordOp_projector('II0')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "722afb93",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.033-0.108j |000> +\n",
"-0.365-0.027j |010> +\n",
" 0.566+0.239j |100> +\n",
"-0.143+0.155j |110>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"proj*test"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "cd079f1e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.080-0.181j |000> +\n",
"-0.080-0.181j |001> +\n",
" 0.055+0.056j |100> +\n",
" 0.055+0.056j |101>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"proj_plus = get_PauliwordOp_projector('I0+')\n",
"\n",
"proj_plus*test"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5829214a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
" 0.707+0.000j IIZ +\n",
" 0.707+0.000j IIX"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from symmer.evolution.gate_library import Had\n",
"\n",
"H3 = Had(n_qubits=3, index=2)\n",
"H3"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "04400965",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.114-0.256j |000> +\n",
" 0.078+0.079j |100>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"H3*proj_plus*test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "008c6411",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
80 changes: 79 additions & 1 deletion symmer/symplectic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,4 +1353,82 @@ def get_computational_projector(N_qubits:int,
sym_arr[:, qubit_inds_to_fix + N_qubits] = binary_vec

projector = PauliwordOp(sym_arr.astype(bool), coeff * sign)
return projector
return projector


def get_PauliwordOp_projector(projector: Union[str, List[str], np.array]) -> "PauliwordOp":
"""
Build PauliwordOp projector onto different qubit states. Using I to leave state unchanged and 0,1,+,-,*,% to fix
qubit.
key:
I leaves qubit unchanged
0,1 fixes qubit as |0>, |1> (Z basis)
+,- fixes qubit as |+>, |-> (X basis)
*,% fixes qubit as |i+>, |i-> (Y basis)
e.g.
'I+0*1II' defines the projector the state I ⊗ [ |+ 0 i+ 1> <+ 0 i+ 1| ] ⊗ II
Args:
projector (str, list) : either string or list of strings defininng projector
TODO: could be used to develop a control version of PauliWordOp
Returns:
projector (PauliwordOp): operator that performs projection
"""
if isinstance(projector, str):
projector = np.array(list(projector))
else:
projector = np.asarray(projector)
basis_dict = {'I':1,
'0':1, '1':-1,
'+':1, '-':-1,
'*':1, '%':-1}
assert len(projector.shape) == 1, 'projector can only be defined over a single string or single list of strings (each a single letter)'
assert set(projector).issubset(list(basis_dict.keys())), 'unknown qubit state (must be I,X,Y,Z basis)'


N_qubits = len(projector)
qubit_inds_to_fix = np.where(projector!='I')[0]
N_qubits_fixed = len(qubit_inds_to_fix)
state_sign = np.array([basis_dict[projector[active_ind]] for active_ind in qubit_inds_to_fix])

if N_qubits_fixed < 64:
binary_vec = (((np.arange(2 ** N_qubits_fixed).reshape([-1, 1]) & (1 << np.arange(N_qubits_fixed))[
::-1])) > 0).astype(int)
else:
binary_vec = (((np.arange(2 ** N_qubits_fixed, dtype=object).reshape([-1, 1]) & (1 << np.arange(N_qubits_fixed,
dtype=object))[
::-1])) > 0).astype(int)

# assign a sign only to 'active positions' (0 in binary not relevent)
sign_from_binary = binary_vec * state_sign

# need to turn 0s in matrix to 1s before taking product across rows
sign_from_binary = sign_from_binary + (sign_from_binary + 1) % 2

sign = np.product(sign_from_binary, axis=1)

coeff = 1 / 2 ** (N_qubits_fixed) * np.ones(2 ** N_qubits_fixed)
sym_arr = np.zeros((coeff.shape[0], 2 * N_qubits))

# assumed in Z basis
sym_arr[:, qubit_inds_to_fix + N_qubits] = binary_vec
sym_arr = sym_arr.astype(bool)

### fix for Y and X basis

X_inds_fixed = np.where(np.logical_or(projector == '+', projector == '-'))[0]
# swap Z block and X block
(sym_arr[:, X_inds_fixed],
sym_arr[:, X_inds_fixed+N_qubits]) = (sym_arr[:, X_inds_fixed+N_qubits],
sym_arr[:, X_inds_fixed].copy())

# copy Z block into X block
Y_inds_fixed = np.where(np.logical_or(projector == '*', projector == '%'))[0]
sym_arr[:, Y_inds_fixed] = sym_arr[:, Y_inds_fixed + N_qubits]

projector = PauliwordOp(sym_arr, coeff * sign)
return projector

0 comments on commit 8248d2c

Please sign in to comment.