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

Physics simulation for Qiskit circuits #165

Merged
merged 29 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7744aca
use name field like single qubit gates
lazyoracle Dec 29, 2021
286edcc
ensure device has enough qubits for circuit
lazyoracle Dec 29, 2021
86b2b5d
ad-hoc hardcoded basis gates
lazyoracle Dec 29, 2021
e9eecd5
create c3 experiment object and compute propagators
lazyoracle Dec 29, 2021
52eb787
convert and evaluate instructions in qasm style
lazyoracle Dec 29, 2021
f21f7c3
remove redundant conversion to c3 legacy style instructions
lazyoracle Dec 29, 2021
4309f67
bump versions and remove mypy dependencies
lazyoracle Dec 29, 2021
7ebfeac
config file for qiskit physics simulations
lazyoracle Dec 29, 2021
0ce0dd9
basic test for qiskit physics simulation
lazyoracle Dec 29, 2021
a83f47b
process circuit and obtain output probabilities
lazyoracle Dec 29, 2021
6b9d1b6
cleanup TODOs and leftovers
lazyoracle Dec 29, 2021
d31b750
show circuit output for debugging
lazyoracle Dec 29, 2021
f11aef6
ignore mypy warning in pwc
lazyoracle Dec 29, 2021
0370d01
create a local circuit for testing
lazyoracle Dec 29, 2021
6ad4c52
update model to 3 qubit device
lazyoracle Dec 29, 2021
53615e4
update parser tests to 3 qubit model
lazyoracle Dec 29, 2021
e19e6bf
provide 3 qubit circuit and result as fixture
lazyoracle Dec 29, 2021
e8c30a2
update qiskit test to 3 qubit circuit
lazyoracle Dec 29, 2021
8d130ef
correct error for too many qubits
lazyoracle Dec 29, 2021
2e60ba9
update TODOs
lazyoracle Dec 29, 2021
7ad01c4
add test for too many qubits
lazyoracle Dec 29, 2021
1ca3732
add physics simulator to parameters
lazyoracle Dec 29, 2021
f1b7494
update generator and model from tests
lazyoracle Dec 29, 2021
ee4ee2a
create 3 qubit circuit because labels are flipped
lazyoracle Dec 29, 2021
b03054a
config for qiskit physics simulation
lazyoracle Dec 29, 2021
b2221dd
update example to use physics simulation
lazyoracle Dec 29, 2021
4383500
docstrings in qiskit tests
lazyoracle Dec 29, 2021
d5da67f
add qiskit physics sim to CHANGELOG
lazyoracle Dec 29, 2021
eac7c24
remove TODO for physics simulator
lazyoracle Dec 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 4 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
repos:
- repo: https://github.com/ambv/black
rev: 21.5b1
rev: 21.12b0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
rev: 4.0.1
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.812'
rev: 'v0.930'
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: [cma==3.0.3,
numpy==1.19.5,
scipy==1.5.2,
tensorflow==2.4.2,
tensorflow-probability==0.12.1,
tensorflow-estimator==2.4.0
]

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ This Changelog tracks all past changes to this project as well as details about
- `fixed` for any bug fixes.
- `security` in case of vulnerabilities.

## Upcoming Version

### Details

- `added` proper physics simulation of circuits using qiskit interface #165

## Version `1.4` - 23 Dec 2021

### Summary
Expand Down
2 changes: 1 addition & 1 deletion c3/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def quick_setup(self, cfg) -> None:
qubit_1 = model.subsystems[props["qubit_1"]]
qubit_2 = model.subsystems[props["qubit_2"]]
instr = Instruction(
name=gate_name,
name=props["name"],
targets=[
model.names.index(props["qubit_1"]),
model.names.index(props["qubit_2"]),
Expand Down
4 changes: 2 additions & 2 deletions c3/libraries/propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _get_hs_of_t_ts(
ts = tf.constant(tf.math.reduce_mean(ts_list, axis=0))
if not np.all(tf.math.reduce_variance(ts_list, axis=0) < 1e-5 * (ts[1] - ts[0])):
raise Exception("C3Error:Something with the times happend.")
if not np.all(tf.math.reduce_variance(ts[1:] - ts[:-1]) < 1e-5 * (ts[1] - ts[0])):
if not np.all(tf.math.reduce_variance(ts[1:] - ts[:-1]) < 1e-5 * (ts[1] - ts[0])): # type: ignore
raise Exception("C3Error:Something with the times happend.")

dt = tf.constant(ts[1 * prop_res].numpy() - ts[0].numpy(), dtype=tf.complex128)
Expand Down Expand Up @@ -263,7 +263,7 @@ def pwc(model: Model, gen: Generator, instr: Instruction) -> Dict:
):
raise Exception("C3Error:Something with the times happend.")
if not np.all(
tf.math.reduce_variance(ts[1:] - ts[:-1]) < 1e-5 * (ts[1] - ts[0])
tf.math.reduce_variance(ts[1:] - ts[:-1]) < 1e-5 * (ts[1] - ts[0]) # type: ignore
):
raise Exception("C3Error:Something with the times happend.")

Expand Down
45 changes: 23 additions & 22 deletions c3/qiskit/c3_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ def run_experiment(self, experiment: QasmQobjExperiment) -> Dict[str, Any]:

# initialise parameters
self._number_of_qubits = len(pmap.model.subsystems)
if self._number_of_qubits != experiment.config.n_qubits:
raise C3QiskitError("Number of qubits in Circuit & Device don't match")
if self._number_of_qubits < experiment.config.n_qubits:
raise C3QiskitError("Not enough qubits on device to run circuit")

shots = self._shots

Expand Down Expand Up @@ -453,7 +453,6 @@ def run_experiment(self, experiment: QasmQobjExperiment) -> Dict[str, Any]:


class C3QasmPhysicsSimulator(C3QasmSimulator):
# TODO Boilerplate code. This simulator is not yet implemented.
"""A C3-based perfect gates simulator for Qiskit

Parameters
Expand All @@ -474,9 +473,12 @@ class C3QasmPhysicsSimulator(C3QasmSimulator):
"open_pulse": False,
"memory": False,
"max_shots": 65536,
"coupling_map": None,
"coupling_map": None, # TODO Coupling map from config file
"description": "A physics based c3 simulator for qasm experiments",
"basis_gates": [], # TODO add basis gates
"basis_gates": [ # TODO Basis gates from config file
"cx",
"x",
],
"gates": [],
}

Expand Down Expand Up @@ -544,14 +546,15 @@ def run_experiment(self, experiment: QasmQobjExperiment) -> Dict[str, Any]:

# setup C3 Experiment
exp = Experiment()
exp.quick_setup(self._device_config)
exp.load_quick_setup(self._device_config)
exp.enable_qasm()
exp.compute_propagators() # TODO only simulate qubits used in circuit
pmap = exp.pmap
model = pmap.model # noqa

# initialise parameters
self._number_of_qubits = len(pmap.model.subsystems)
if self._number_of_qubits != experiment.config.n_qubits:
raise C3QiskitError("Number of qubits in Circuit & Device dont match")
if self._number_of_qubits < experiment.config.n_qubits:
raise C3QiskitError("Not enough qubits on device to run circuit")

shots = self._shots # noqa

Expand All @@ -564,24 +567,22 @@ def run_experiment(self, experiment: QasmQobjExperiment) -> Dict[str, Any]:
# TODO set simulator seed, check qiskit python qasm simulator
# qiskit-terra/qiskit/providers/basicaer/qasm_simulator.py
seed_simulator = 2441129
instructions_list = [
instruction.to_dict() for instruction in experiment.instructions
]

# convert qasm instruction set to c3 sequence
sequence = get_sequence(experiment.instructions) # noqa

# TODO get_init_ground_state(), compute_propagators(), evaluate(), process()

# generate shots style readout with no SPAM
# TODO a sophisticated readout/measurement routine

# TODO generate state labels using get_labels()
pops = exp.evaluate([instructions_list])
pop1s, _ = exp.process(pops)

# TODO create results dict and remove empty states
counts = {} # type: ignore
# TODO generate shots style readout ref Perfect Simulator
# TODO a sophisticated readout/measurement routine (w/ SPAM)
# C3 stores labels in exp.pmap.model.state_labels
counts = dict(zip(self.get_labels(), pop1s[0].numpy().tolist()))

# flipping state labels to match qiskit style qubit indexing convention
# default is to flip labels to qiskit style, use disable_flip_labels()
if self._flip_labels:
counts = flip_labels(counts)
# if self._flip_labels:
# counts = flip_labels(counts)

end = time.time()

Expand Down