-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add classical simulator #6124
Merged
NoureldinYosri
merged 125 commits into
quantumlib:master
from
naerabati:add_classical_simulator
Nov 13, 2023
Merged
Add classical simulator #6124
Changes from 116 commits
Commits
Show all changes
125 commits
Select commit
Hold shift + click to select a range
87dc8f4
added the basic first version of the classical simulator to my branch
naerabati 1b7ab94
added formatting
naerabati 28a1049
edited the qubit order and tests
naerabati 21efeaa
added more test cases
naerabati 152e054
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati fa1102c
fixed some error in the test file
naerabati 9795119
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 041c293
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 7cf6b87
added param_resolver and test cases
naerabati 8bff310
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati df7bfcc
changed mutliple_instances test
naerabati da44625
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 9559af9
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 2f3d765
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 36fcce5
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 3b7bc89
updated comments
naerabati 95800f3
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 690ef69
updated inputs
naerabati 867f6cf
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 9f9c760
formatted files
naerabati 154dc72
deleted vs files
naerabati 1f3f321
changed comments
naerabati 3d700e6
updated commenst
naerabati 16738e9
changed the import statements
naerabati 7466c3c
updated imports
naerabati bab3b0b
changed comments
naerabati e4b1f58
changed spacing
naerabati 5146a44
updated spacing
naerabati 1d7744f
changed comments
naerabati 0a486b2
updated self argument
naerabati a5723eb
changed imports
naerabati f2f2c60
changed imports
naerabati 30855ee
changed imports
naerabati a8bc3c5
updated imports
naerabati ded44ed
updated imports
naerabati 01aa246
updated spacing
naerabati 15924e7
changed arguments
naerabati 12e0571
changed arguments
naerabati 68c85ff
updated arguments
naerabati 886944f
updated arguments
naerabati a368eac
updated files
naerabati 4f069a8
updated file
naerabati 53d918e
changed format
naerabati 9546e79
added self
naerabati 5cf16bb
formatted
naerabati d30d536
added swap
naerabati 62aabfd
added swap
naerabati 60a0ce1
added swap test
naerabati c87689c
added CCNOT gate
naerabati 669d33c
formatted
naerabati 3338e7c
updated test case for CCNOT
naerabati be30e5e
merge changes from master into add_classical simulator
naerabati 686c502
reverted files
naerabati b2f7835
reverted files
naerabati 61e4c98
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 7710ef2
reverted files
naerabati 8814187
added to should not be serialized
naerabati 7d1677d
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 962b8b2
changed syntax
naerabati d7c4d3c
reformatted
naerabati 7c68107
updated test
naerabati 7d29f8b
formatted
naerabati baeeee0
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati ef4831a
updated format
naerabati 1f9de2c
changed format
naerabati f54d232
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 8fa47d0
fixed long lines
naerabati 866bc4e
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati fc85691
merged with master
naerabati 07962bb
reformatted
naerabati be240e8
updated comments
naerabati 3403f9a
reformatted
naerabati 6b64f7b
updated imports
naerabati 3c8798c
Merge branch 'master' into add_classical_simulator
naerabati c9b7741
Merge branch 'master' into add_classical_simulator
naerabati 7dc9213
Merge branch 'master' into add_classical_simulator
naerabati 136e3c0
Merge branch 'master' into add_classical_simulator
naerabati 20bb30e
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 2d436f6
Merge branch 'master' into add_classical_simulator
naerabati 57d0770
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 5b522f6
Merge branch 'master' into add_classical_simulator
naerabati eac993e
Merge branch 'master' into add_classical_simulator
naerabati 90f9855
restored file
naerabati 36d4391
Merge branch 'master' into add_classical_simulator
naerabati f284513
changed tests to sim.run function
naerabati 732234e
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 97946c6
removed, checking for gate exponenets of 0
naerabati 9f08a2d
formatted
naerabati dbb83d7
reverted back to _run
naerabati 2d8b903
reverted unitary_protocol_test to master
naerabati 0edea6c
formatted
naerabati ba426f8
changed parameter name
naerabati eccf29b
forma
naerabati 89a52ae
formatted
naerabati 464dd53
updated test
naerabati e32cdb2
Merge branch 'master' into add_classical_simulator
naerabati ad62928
fixed line too long
naerabati 0a72999
Merge branch 'add_classical_simulator' of https://github.com/naerabat…
naerabati 1ef4f52
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 1b612f1
moved parenthesis
naerabati b22c47e
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 77c6430
added test records
naerabati 11f7803
test dictionary
naerabati 2ca4c11
changed argument
naerabati de7d1c4
changed format
naerabati e32a046
fixed format
naerabati 4cdd45d
changed measurements
naerabati 399ff68
test dictionary
naerabati 6477270
update dictionary
naerabati ed0ae54
test dictionary
naerabati 749200e
updated records
naerabati a62de00
changed circuit parameters
naerabati 0b258a2
updated circuit parameter
naerabati 21a7f9d
updated test_unknown_gate value
naerabati 2c7e003
updated line indent
naerabati 6168c75
Merge branch 'quantumlib:master' into add_classical_simulator
naerabati 93d16f7
restore unrelated files to their original state
naerabati c09aeca
restore unrelated files to their original state
naerabati d479fb2
added _ = for test case unassigned line
naerabati 2b9c078
restored files
naerabati c3b557e
Merge branch 'master' into add_classical_simulator
NoureldinYosri 9e6c138
remove .vs files
NoureldinYosri 41bb80e
restore unrelated files to their original state
NoureldinYosri 3bc22cf
fix style
NoureldinYosri c8cd782
Merge branch 'master' into add_classical_simulator
NoureldinYosri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -50,3 +50,6 @@ docs/api_docs | |
|
||
# notebook test output | ||
out | ||
|
||
#ignore vs files | ||
.vs/* |
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
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
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
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
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 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Dict | ||
from collections import defaultdict | ||
from cirq.sim.simulator import SimulatesSamples | ||
from cirq import ops, protocols | ||
from cirq.study.resolver import ParamResolver | ||
from cirq.circuits.circuit import AbstractCircuit | ||
from cirq.ops.raw_types import Qid | ||
import numpy as np | ||
|
||
|
||
class ClassicalStateSimulator(SimulatesSamples): | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""basic simulator that only accepts cirq.X, cirq.ISwap, and cirq.CNOT gates | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please replace the docstring with """A simulator that only accepts only gates with classical counterparts.
This simulator evolves a single state, using only gates that output a single state for each
input state. The simulator runs in linear time, at the cost of not supporting superposition.
It can be used to estimate costs and simulate circuits for simple non-quantum algorithms using
many more qubits than fully capable quantum simulators.
The supported gates are:
- cirq.X
- cirq.CNOT
- cirq.SWAP
- cirq.TOFFOLI
- cirq.measure
Args:
circuit: The circuit to simulate.
param_resolver: Parameters to run with the program.
repetitions: Number of times to repeat the run. It is expected that
this is validated greater than zero before calling this method.
Returns:
A dictionary mapping measurement keys to measurement results.
Raises:
ValueError: If one of the gates is not an X, CNOT, SWAP, TOFFOLI or a measurement.
""" |
||
|
||
Run a simulation, mimicking quantum hardware. | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
circuit: The circuit to simulate. | ||
param_resolver: Parameters to run with the program. | ||
repetitions: Number of times to repeat the run. It is expected that | ||
this is validated greater than zero before calling this method. | ||
|
||
Returns: | ||
A dictionary from measurement gate key to measurement | ||
results. Measurement results are stored in a 3-dimensional | ||
numpy array, the first dimension corresponding to the repetition. | ||
the second to the instance of that key in the circuit, and the | ||
third to the actual boolean measurement results (ordered by the | ||
qubits being measured.) | ||
|
||
Raises: | ||
ValuesError: gate is not a cirq.XGate or cirq.Cnot | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
def _run( | ||
self, circuit: AbstractCircuit, param_resolver: ParamResolver, repetitions: int | ||
) -> Dict[str, np.ndarray]: | ||
results_dict: Dict[str, np.ndarray] = {} | ||
values_dict: Dict[Qid, int] = defaultdict(int) | ||
param_resolver = param_resolver or ParamResolver({}) | ||
resolved_circuit = protocols.resolve_parameters(circuit, param_resolver) | ||
|
||
for moment in resolved_circuit: | ||
for op in moment: | ||
gate = op.gate | ||
if gate == ops.X: | ||
values_dict[op.qubits[0]] = 1 - values_dict[op.qubits[0]] | ||
|
||
elif ( | ||
isinstance(gate, ops.CNotPowGate) | ||
and gate.exponent == 1 | ||
and gate.global_shift == 0 | ||
): | ||
if values_dict[op.qubits[0]] == 1: | ||
values_dict[op.qubits[1]] = 1 - values_dict[op.qubits[1]] | ||
|
||
dstrain115 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elif ( | ||
isinstance(gate, ops.SwapPowGate) | ||
and gate.exponent == 1 | ||
and gate.global_shift == 0 | ||
): | ||
hold_qubit = values_dict[op.qubits[1]] | ||
values_dict[op.qubits[1]] = values_dict[op.qubits[0]] | ||
values_dict[op.qubits[0]] = hold_qubit | ||
|
||
elif ( | ||
isinstance(gate, ops.CCXPowGate) | ||
and gate.exponent == 1 | ||
and gate.global_shift == 0 | ||
): | ||
if (values_dict[op.qubits[0]] == 1) and (values_dict[op.qubits[1]] == 1): | ||
values_dict[op.qubits[2]] = 1 - values_dict[op.qubits[2]] | ||
|
||
elif isinstance(gate, ops.MeasurementGate): | ||
qubits_in_order = op.qubits | ||
# add the new instance of a key to the numpy array in results dictionary | ||
if gate.key in results_dict: | ||
shape = len(qubits_in_order) | ||
current_array = results_dict[gate.key] | ||
new_instance = np.zeros(shape, dtype=np.uint8) | ||
for bits in range(0, len(qubits_in_order)): | ||
new_instance[bits] = values_dict[qubits_in_order[bits]] | ||
results_dict[gate.key] = np.insert( | ||
current_array, len(current_array[0]), new_instance, axis=1 | ||
) | ||
else: | ||
# create the array for the results dictionary | ||
new_array_shape = (repetitions, 1, len(qubits_in_order)) | ||
new_array = np.zeros(new_array_shape, dtype=np.uint8) | ||
for reps in range(0, repetitions): | ||
for instances in range(1): | ||
for bits in range(0, len(qubits_in_order)): | ||
new_array[reps][instances][bits] = values_dict[ | ||
qubits_in_order[bits] | ||
] | ||
results_dict[gate.key] = new_array | ||
|
||
elif not ( | ||
(isinstance(gate, ops.XPowGate) and gate.exponent == 0) | ||
or (isinstance(gate, ops.CCXPowGate) and gate.exponent == 0) | ||
or (isinstance(gate, ops.SwapPowGate) and gate.exponent == 0) | ||
or (isinstance(gate, ops.CNotPowGate) and gate.exponent == 0) | ||
): | ||
raise ValueError( | ||
"Can not simulate gates other than cirq.XGate, " | ||
+ "cirq.CNOT, cirq.SWAP, and cirq.CCNOT" | ||
) | ||
|
||
return results_dict |
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,196 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import numpy as np | ||
import pytest | ||
import cirq | ||
import sympy | ||
|
||
|
||
class TestSimulator: | ||
NoureldinYosri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_x_gate(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.X(q1)) | ||
circuit.append(cirq.X(q1)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[1, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_CNOT(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.measure(q1, key='key')) | ||
expected_results = {'key': np.array([[[1]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_Swap(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.SWAP(q0, q1)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[0, 1]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_CCNOT(self): | ||
q0, q1, q2 = cirq.LineQubit.range(3) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.CCNOT(q0, q1, q2)) | ||
circuit.append(cirq.measure((q0, q1, q2), key='key')) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CCNOT(q0, q1, q2)) | ||
circuit.append(cirq.measure((q0, q1, q2), key='key')) | ||
circuit.append(cirq.X(q1)) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CCNOT(q0, q1, q2)) | ||
circuit.append(cirq.measure((q0, q1, q2), key='key')) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CCNOT(q0, q1, q2)) | ||
circuit.append(cirq.measure((q0, q1, q2), key='key')) | ||
expected_results = { | ||
'key': np.array([[[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 1]]], dtype=np.uint8) | ||
} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_measurement_gate(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[0, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_qubit_order(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[1, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_same_key_instances(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[0, 0], [1, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_same_key_instances_order(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.measure((q1, q0), key='key')) | ||
expected_results = {'key': np.array([[[1, 0], [0, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_repetitions(self): | ||
q0 = cirq.LineQubit.range(1) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.measure(q0, key='key')) | ||
expected_results = { | ||
'key': np.array( | ||
[[[0]], [[0]], [[0]], [[0]], [[0]], [[0]], [[0]], [[0]], [[0]], [[0]]], | ||
dtype=np.uint8, | ||
) | ||
} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=10).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_multiple_gates(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.X(q1)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[1, 0]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_multiple_gates_order(self): | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(cirq.CNOT(q0, q1)) | ||
circuit.append(cirq.CNOT(q1, q0)) | ||
circuit.append(cirq.measure((q0, q1), key='key')) | ||
expected_results = {'key': np.array([[[0, 1]]], dtype=np.uint8)} | ||
sim = cirq.ClassicalStateSimulator() | ||
results = sim.run(circuit, param_resolver=None, repetitions=1).records | ||
np.testing.assert_equal(results, expected_results) | ||
|
||
def test_param_resolver(self): | ||
gate = cirq.CNOT ** sympy.Symbol('t') | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(cirq.X(q0)) | ||
circuit.append(gate(q0, q1)) | ||
circuit.append(cirq.measure((q1), key='key')) | ||
resolver = cirq.ParamResolver({'t': 0}) | ||
sim = cirq.ClassicalStateSimulator() | ||
results_with_parameter_zero = sim.run( | ||
circuit, param_resolver=resolver, repetitions=1 | ||
).records | ||
resolver = cirq.ParamResolver({'t': 1}) | ||
results_with_parameter_one = sim.run( | ||
circuit, param_resolver=resolver, repetitions=1 | ||
).records | ||
np.testing.assert_equal( | ||
results_with_parameter_zero, {'key': np.array([[[0]]], dtype=np.uint8)} | ||
) | ||
np.testing.assert_equal( | ||
results_with_parameter_one, {'key': np.array([[[1]]], dtype=np.uint8)} | ||
) | ||
|
||
def test_unknown_gates(self): | ||
gate = cirq.CNOT ** sympy.Symbol('t') | ||
q0, q1 = cirq.LineQubit.range(2) | ||
circuit = cirq.Circuit() | ||
circuit.append(gate(q0, q1)) | ||
circuit.append(cirq.measure((q0), key='key')) | ||
resolver = cirq.ParamResolver({'t': 0.5}) | ||
sim = cirq.ClassicalStateSimulator() | ||
with pytest.raises( | ||
ValueError, | ||
match="Can not simulate gates other than " | ||
+ "cirq.XGate, cirq.CNOT, cirq.SWAP, and cirq.CCNOT", | ||
): | ||
sim.run(circuit, param_resolver=resolver, repetitions=1).records | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to make any changes to this file or to the
.gitignore
file. please dogit checkout master cirq-core/cirq/protocols/unitary_protocol_test.py git checkout master .gitignore git add cirq-core/cirq/protocols/unitary_protocol_test.py .gitignore git commit -m "restore unrelated files to their original state"