Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new method to return the final state vector array instead of wrapping it #623

Merged
merged 37 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6d12636
allow representing simulation results as 1D array
NoureldinYosri Sep 21, 2023
6705a06
add debug line
NoureldinYosri Sep 22, 2023
736d45b
Debug line
NoureldinYosri Sep 22, 2023
8c84f39
Debug line
NoureldinYosri Sep 22, 2023
6337d59
Debug line
NoureldinYosri Sep 22, 2023
9b9e426
Docker Debug line
NoureldinYosri Sep 22, 2023
2c7c06b
Docker Debug line
NoureldinYosri Sep 22, 2023
585a996
Docker Debug line
NoureldinYosri Sep 22, 2023
16b5ab6
Docker Debug line
NoureldinYosri Sep 22, 2023
575ba8d
Docker Debug line
NoureldinYosri Sep 22, 2023
7cdda0c
Docker Debug line
NoureldinYosri Sep 22, 2023
5794472
Docker Debug line
NoureldinYosri Sep 22, 2023
d16dfa0
Docker Debug line
NoureldinYosri Sep 22, 2023
a1e5a22
revert changes to docker file
NoureldinYosri Sep 22, 2023
0009bc4
change into adding a new functionality
NoureldinYosri Sep 22, 2023
1277509
update return type
NoureldinYosri Sep 25, 2023
8627951
updae
NoureldinYosri Sep 25, 2023
6c17d8d
docs
NoureldinYosri Sep 25, 2023
7de5803
try to fix CI
NoureldinYosri Oct 2, 2023
7bad066
try to fix CI
NoureldinYosri Oct 2, 2023
fab46a2
try to fix CI
NoureldinYosri Oct 2, 2023
b437035
try to fix CI
NoureldinYosri Oct 2, 2023
3b7c878
try to fix CI
NoureldinYosri Oct 2, 2023
30441c8
try to fix CI
NoureldinYosri Oct 2, 2023
fe0090a
try to fix CI
NoureldinYosri Oct 2, 2023
c1dfbd1
try to fix CI
NoureldinYosri Oct 2, 2023
72bf545
try to fix CI
NoureldinYosri Oct 2, 2023
2600dac
try to fix CI
NoureldinYosri Oct 2, 2023
393a7a3
try to fix CI
NoureldinYosri Oct 2, 2023
510e4f0
try to fix CI
NoureldinYosri Oct 2, 2023
1bd151c
fix docker file
NoureldinYosri Oct 2, 2023
e8df883
upgrade bazel version
NoureldinYosri Oct 3, 2023
cb32edb
restore bazel version
NoureldinYosri Oct 3, 2023
6b159a9
Merge branch 'master' into np32
NoureldinYosri Oct 18, 2023
2da0548
rerun CI
NoureldinYosri Oct 18, 2023
343895c
replace upgrade with satisfy
NoureldinYosri Oct 18, 2023
a1025aa
upgrade libc to latests
NoureldinYosri Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pybind_interface/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ FROM qsim
# Install additional requirements
RUN apt-get install -y python3-dev python3-pybind11 python3-pytest python3-pip

# The --force flag is used mainly so that the old numpy installation from pybind
# The --force-reinstall flag is used mainly so that the old numpy installation from pybind
# gets replaced with the one cirq requires
RUN pip3 install --prefer-binary cirq-core --force
RUN pip3 install --break-system-packages --prefer-binary cirq-core --force-reinstall

# Copy relevant files
COPY ./pybind_interface/ /qsim/pybind_interface/
Expand Down
60 changes: 59 additions & 1 deletion qsimcirq/qsim_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,67 @@ def compute_amplitudes_sweep_iter(
options["s"] = self.get_seed()
yield simulator_fn(options)

def simulate(
NoureldinYosri marked this conversation as resolved.
Show resolved Hide resolved
self,
program: cirq.AbstractCircuit,
param_resolver: cirq.ParamResolverOrSimilarType = None,
qubit_order: cirq.QubitOrderOrList = cirq.ops.QubitOrder.DEFAULT,
initial_state: Any = None,
as_1d_state_vector: bool = False,
) -> cirq.sim.simulator.TSimulationTrialResult:
"""Simulates the supplied Circuit.

This method returns a result which allows access to the entire
simulator's final state.

Args:
program: The circuit to simulate.
param_resolver: Parameters to run with the program.
qubit_order: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
initial_state: The initial state for the simulation. The form of
this state depends on the simulation implementation. See
documentation of the implementing class for details.
as_1d_state_vector: Whether the returned state vector is 1D or
has number of dimensions equal number of qubits.
Operations on 1D representation are significantly slower than
the other representation and might not even work.
The 1D representation should only be used when the number of qubits
is larger than the dimension limit on numpy arrays (numpy/numpy#5744).

Returns:
SimulationTrialResults for the simulation. Includes the final state.
"""
return self.simulate_sweep(
program,
cirq.study.ParamResolver(param_resolver),
qubit_order,
initial_state,
as_1d_state_vector,
)[0]

def simulate_sweep(
self,
program: cirq.AbstractCircuit,
params: cirq.Sweepable,
qubit_order: cirq.QubitOrderOrList = cirq.ops.QubitOrder.DEFAULT,
initial_state: Any = None,
as_1d_state_vector: bool = False,
) -> List[cirq.sim.simulator.TSimulationTrialResult]:
return list(
self.simulate_sweep_iter(
program, params, qubit_order, initial_state, as_1d_state_vector
)
)

def simulate_sweep_iter(
self,
program: cirq.Circuit,
params: cirq.Sweepable,
qubit_order: cirq.QubitOrderOrList = cirq.QubitOrder.DEFAULT,
initial_state: Optional[Union[int, np.ndarray]] = None,
as_1d_state_vector: bool = False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates the API defined by cirq.SimulatesFinalState.simulate_sweep_iter. If there are plans to modify that function as well, please link the relevant Cirq PR. (The required cirq version for qsim will also need to be updated if this is the case.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't violate the API. but it changes the internal data representation just for this if and only if as_1d_state_vector = True which by default is False so nothing changes unless the caller explicitly wants the 1D representation. This is done only for the qsim simulator.

The 1D representation is used for only one reason and that is to report the result, because the normal representation is a tensor that has number of dimensions equal to the num_qubits which breaks when the number of qubits is greater than the limit on numpy array dimensions (see issue in docstring & PR description)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QSimSimulator inherits from cirq.SimulatesFinalState, whose simulate_sweep_iter method does not have this new argument. Even though the implementation here can accept any valid input to the function of the parent class, it's still a violation of the API.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the approach to adding a new method rather than extending the existing API.

) -> Iterator[cirq.StateVectorTrialResult]:
"""Simulates the supplied Circuit.

Expand All @@ -463,6 +518,8 @@ def simulate_sweep_iter(
be an integer representing a pure state (e.g. 11010) or a numpy
array containing the full state vector. If none is provided, this
is assumed to be the all-zeros state.
as_1d_state_vector: Whether the returned state vector is 1D or
has number of dimensions equal number of qubits.

Returns:
List of SimulationTrialResults for this run, one for each
Expand Down Expand Up @@ -527,7 +584,8 @@ def simulate_sweep_iter(
assert qsim_state.ndim == 1

final_state = cirq.StateVectorSimulationState(
initial_state=qsim_state.view(np.complex64), qubits=cirq_order
initial_state=qsim_state.view(np.complex64),
qubits=cirq_order if not as_1d_state_vector else None,
)
# create result for this parameter
# TODO: We need to support measurements.
Expand Down
12 changes: 12 additions & 0 deletions qsimcirq_tests/qsimcirq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,3 +2058,15 @@ def test_cirq_global_phase_gate():
assert cirq.approx_eq(
qsim_result.state_vector(), cirq_result.state_vector(), atol=1e-6
)


def test_1d_representation():
qsim_sim = qsimcirq.QSimSimulator()
qs = cirq.LineQubit.range(2)
c = cirq.Circuit(cirq.H.on_each(qs), cirq.X(qs[0]), cirq.Y(qs[1]))

want = np.array([0.0 - 0.5j, 0.0 + 0.5j, 0.0 - 0.5j, 0.0 + 0.5j])
res = qsim_sim.simulate(c, as_1d_state_vector=True)
np.testing.assert_allclose(
res.final_state_vector, np.array(want, dtype=np.complex64)
)
Loading