Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-in-python/quantum/deutsch_jozsa.py at flake8-bugbear · coderjack/algorithms-in-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
coderjack
/
algorithms-in-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
flake8-bugbear
Breadcrumbs
algorithms-in-python
/
quantum
/
deutsch_jozsa.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
122 lines (101 loc) · 4.33 KB
flake8-bugbear
Breadcrumbs
algorithms-in-python
/
quantum
/
deutsch_jozsa.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
122 lines (101 loc) · 4.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""
Deutsch-Josza Algorithm is one of the first examples of a quantum
algorithm that is exponentially faster than any possible deterministic
classical algorithm
Premise:
We are given a hidden Boolean function f,
which takes as input a string of bits, and returns either 0 or 1:
f({x0,x1,x2,...}) -> 0 or 1, where xn is 0 or 1
The property of the given Boolean function is that it is guaranteed to
either be balanced or constant. A constant function returns all 0's
or all 1's for any input, while a balanced function returns 0's for
exactly half of all inputs and 1's for the other half. Our task is to
determine whether the given function is balanced or constant.
References:
- https://en.wikipedia.org/wiki/Deutsch-Jozsa_algorithm
- https://qiskit.org/textbook/ch-algorithms/deutsch-jozsa.html
"""
import
numpy
as
np
import
qiskit
as
q
def
dj_oracle
(
case
:
str
,
num_qubits
:
int
)
->
q
.
QuantumCircuit
:
"""
Returns a Quantum Circuit for the Oracle function.
The circuit returned can represent balanced or constant function,
according to the arguments passed
"""
# This circuit has num_qubits+1 qubits: the size of the input,
# plus one output qubit
oracle_qc
=
q
.
QuantumCircuit
(
num_qubits
+
1
)
# First, let's deal with the case in which oracle is balanced
if
case
==
"balanced"
:
# First generate a random number that tells us which CNOTs to
# wrap in X-gates:
b
=
np
.
random
.
randint
(
1
,
2
**
num_qubits
)
# Next, format 'b' as a binary string of length 'n', padded with zeros:
b_str
=
format
(
b
,
f"0
{
num_qubits
}
b"
)
# Next, we place the first X-gates. Each digit in our binary string
# correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
# we apply an X-gate to that qubit:
for
index
,
bit
in
enumerate
(
b_str
):
if
bit
==
"1"
:
oracle_qc
.
x
(
index
)
# Do the controlled-NOT gates for each qubit, using the output qubit
# as the target:
for
index
in
range
(
num_qubits
):
oracle_qc
.
cx
(
index
,
num_qubits
)
# Next, place the final X-gates
for
index
,
bit
in
enumerate
(
b_str
):
if
bit
==
"1"
:
oracle_qc
.
x
(
index
)
# Case in which oracle is constant
if
case
==
"constant"
:
# First decide what the fixed output of the oracle will be
# (either always 0 or always 1)
output
=
np
.
random
.
randint
(
2
)
if
output
==
1
:
oracle_qc
.
x
(
num_qubits
)
oracle_gate
=
oracle_qc
.
to_gate
()
oracle_gate
.
name
=
"Oracle"
# To show when we display the circuit
return
oracle_gate
def
dj_algorithm
(
oracle
:
q
.
QuantumCircuit
,
num_qubits
:
int
)
->
q
.
QuantumCircuit
:
"""
Returns the complete Deustch-Jozsa Quantum Circuit,
adding Input & Output registers and Hadamard & Measurement Gates,
to the Oracle Circuit passed in arguments
"""
dj_circuit
=
q
.
QuantumCircuit
(
num_qubits
+
1
,
num_qubits
)
# Set up the output qubit:
dj_circuit
.
x
(
num_qubits
)
dj_circuit
.
h
(
num_qubits
)
# And set up the input register:
for
qubit
in
range
(
num_qubits
):
dj_circuit
.
h
(
qubit
)
# Let's append the oracle gate to our circuit:
dj_circuit
.
append
(
oracle
,
range
(
num_qubits
+
1
))
# Finally, perform the H-gates again and measure:
for
qubit
in
range
(
num_qubits
):
dj_circuit
.
h
(
qubit
)
for
i
in
range
(
num_qubits
):
dj_circuit
.
measure
(
i
,
i
)
return
dj_circuit
def
deutsch_jozsa
(
case
:
str
,
num_qubits
:
int
)
->
q
.
result
.
counts
.
Counts
:
"""
Main function that builds the circuit using other helper functions,
runs the experiment 1000 times & returns the resultant qubit counts
>>> deutsch_jozsa("constant", 3)
{'000': 1000}
>>> deutsch_jozsa("balanced", 3)
{'111': 1000}
"""
# Use Aer's qasm_simulator
simulator
=
q
.
Aer
.
get_backend
(
"qasm_simulator"
)
oracle_gate
=
dj_oracle
(
case
,
num_qubits
)
dj_circuit
=
dj_algorithm
(
oracle_gate
,
num_qubits
)
# Execute the circuit on the qasm simulator
job
=
q
.
execute
(
dj_circuit
,
simulator
,
shots
=
1000
)
# Return the histogram data of the results of the experiment.
return
job
.
result
().
get_counts
(
dj_circuit
)
if
__name__
==
"__main__"
:
print
(
f"Deutsch Jozsa - Constant Oracle:
{
deutsch_jozsa
(
'constant'
,
3
)
}
"
)
print
(
f"Deutsch Jozsa - Balanced Oracle:
{
deutsch_jozsa
(
'balanced'
,
3
)
}
"
)
You can’t perform that action at this time.